Simple ember component based form validation module , only providing base structure and components for validation . His goal is to be flexible and adaptive to any situation.
- Ember.js v2.18 or above
- Ember CLI v2.13 or above
- Node.js v8 or above
ember install ember-base-form-validation
- Async validation
- Component level validation
- You choose when to validate and at which level
The validation form is the base of the validation , it must contain a @schema
attribute in order to provide validation to the inputs.
userform.hbs
<ValidationForm @validateOnInit={{false}} class="form" @schema={{this.validation}} as |form|>
</ValidationForm>
@schema
(required) : aValidation schema
for the children inputs@validateOnInit
(optional) : aboolean
to tell the form to validate or not all the children on init.any html attributes
(optional)
validate
: runs the validation for all the children
isDirty
(boolean
) : returns if the form is dirty (any field has been validated)hasErrors
(boolean
) : returns if the form validator has errorsvalidating
(boolean
) : returns if the form validator is running validations (for async).
The validation input is an HTML input who validates its value after some events had been triggered.
<ValidationForm class="form" @schema={{this.validation}} as |form|>
<ValidationInput @validation="username" @validateOn="change" name="username" @parent={{form}} as |i|>
{{#if i.error}}
<p>{{i.error}}</p>
{{/if}}
</ValidationInput>
<ValidationInput @validation="email" @validateOn="focus" name="email" @parent={{form}} as |i|>
{{#if i.error}}
<p>{{i.error.message}} for {{i.error.field}}</p>
{{/if}}
</ValidationInput>
<input type="submit" disabled={{form.hasErrors}} {{on "click" this.submit}} value="submit">
</ValidationForm>
@parent
(BaseValidationFormComponent
) (required) : the parent form.@validation
(string
) (required) : Tell which validation the validator must use to validate the input value.@validateOn
(string
) (optional) : an html event to tell the input when to launch its validation.@alone
(boolean
) (optional) : disable the validation ,@parent
and@validation
attributes become optional.any html attributes
(optional)
validate
: runs the validation for the input
isDirty
(boolean
) : returns if the input value is dirty (has been validated)error
(any
) : error associated to the input (null
if no error)
The validation schema checks if the current value of the input is correct , otherwhise it returns any value indicating there's an error for the field.
If it returns null
or undefined
, the value is correct.
The method can be async or sync.
import validator from 'validator'; // external validation module
import { BaseValidator , validationProperty } from 'ember-base-form-validation';
export class UserValidator extends BaseValidator {
@validationProperty()
username(str) {
if (!validator.isLength(str,{
min : 10
})) {
return 'Lenght must be less than 10 characters';
}
}
@validationProperty()
async email(str) {
if (!validator.isEmail(str)) {
return { // can return in any format you want
message : 'Email not valid',
field : 'email'
};
}
}
}
@validationProperty(ignoreUndefined = true)
indicates that the method is a validation method. The parameter ignoreUndefined
converts the input value to a null string if it's undefined.
errors
object
: errors of the validatorcontext
any
: context passed by the constructor
-
waitAndCheckErrors
(Promise<boolean>
) : wait for the validator to finish validation and returns if it contains errors. -
hasErrors
(boolean
) : returns if validator has errors -
isDirty
(boolean
) : returns if validator has validated this field at least once -
validationRunning
(boolean
) : returns if validator is running async tasks.
import Component from '@glimmer/component';
import { UserValidator } from '../validation/user';
import { action } from '@ember/object';
export default class UserFormComponent extends Component {
validation;
constructor(...args) {
super(...args);
this.validation = new UserValidator(this);
}
@action
submit(t) {
this.validation.waitAndCheckErrors().then( (hasErrors) => {
if (hasErrors) {
return;
}
// do your job
}
}
}
Same as above except for the template. Custom Input let you define you own input to bind the value to and validate.
userform.hbs
You can inherit BaseValidationInputComponent
class to make your custom input component and BaseValidationFormComponent
to make your own validation form
myinput.js
import { action } from '@ember/object';
import { BaseValidationInputComponent } from 'ember-base-form-validation';
export default class MyinputComponent extends BaseValidationInputComponent {
constructor() {
super(...arguments);
console.log("Init custom");
}
@action
validate() {
super.validate();
console.log("Validate " + this.name);
}
}
myform.js
import { action } from '@ember/object';
import { BaseValidationFormComponent } from 'ember-base-form-validation';
export default class MyformComponent extends BaseValidationFormComponent {
constructor() {
super(...arguments);
console.log("Init custom");
}
@action
validate() {
super.validate();
console.log("Validate all");
}
}
❗ I'm new to EmberJS community , don't hesitate to contribute !
See the Contributing guide for details.
This project is licensed under the MIT License.