Instead of having constraint definitions scattered throughout input/form markup, valdr offers to define constraints and messages in code (or load them from the back-end):
myApp.config(function(valdrProvider) {
valdrProvider.addConstraints({
'Person': {
'firstName': {
'size': {
'min': 3,
'max': 20,
'message': 'First name is required to be between 3 and 20 characters.'
},
'required': {
'message': 'First name is required.'
}
}
});
});
Now use the valdr-type directive to apply those constraints to one or multiple form elements:
<form valdr-type="Person">
<label for="firstName">First Name</label>
<input type="text"
id="firstName"
name="firstName"
ng-model="person.firstName">
</form>
That's it! valdr will validate the firstName input field with the size and the required validator. It will set the $validity of this form item accordingly.
This is a simple demo which uses CSS to show the validation messages only when the field is blurred and invalid.
The demo uses the following constraints:
{{ getConstraints() | json }}
Have a look at the documentation and more demo pages on Github.