API
===
### **Add Valid Message (error, success) for validation `required`**
`required-error-message` and `required-success-message`
```html
```
### **Add Valid Message (error, success) for validation `email`**
`email-error-message` and `email-success-message`
```html
```
### **Use Default Valid Message**
*You don't need to give valid message* - the valid/invalid message will be automatically placed next to your input element.
```html
```
### **Use a custom Valid Message**
You can also add a custom validation message by using `message-id` attribute. It allows you to place a valid/invalid message wherever you want, a target element must specify an `id` attribute that matches with a value of the `message-id`.
```html
```
### **Use a validation group**
You can also add a `validation-group` directive to group many elements into a group. The group will be considered as valid if and only if one of them is valid. Otherwise, the group will be marked as invalid. A valid/invalid message will be placed inside an element that contains an id attribute with the same name as provided to the directive `validation-group`.
```html
```
> Note that the `validation-group` directive can only be used to group elements placed in the same form. In other words, you can't group elements across different `
```
### **Select a global validation method** `watch blur submit submit-only`**
`validationProvider.setValidMethod('submit')`
### **Setup a new Validation `setExpression()` `setDefaultMsg()` with `RegExp` or `Function` in config phase**
```html
```
```javascript
// your module
angular.module('yourApp', ['validation'])
.config(['$validationProvider', function ($validationProvider) {
// Setup `ip` validation
var expression = {
ip: /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
};
var validMsg = {
ip: {
error: 'This isn\'t ip address',
success: 'It\'s ip'
}
};
$validationProvider.setExpression(expression) // set expression
.setDefaultMsg(validMsg); // set valid message
// Setup `huei` validation
$validationProvider
.setExpression({
huei: function (value, scope, element, attrs) {
return value === 'Huei Tan';
// or you can do
return $q.all([obj]).then(function () {
// ...
return true;
})
}
})
.setDefaultMsg({
huei: {
error: 'This should be Huei Tan',
success: 'Thanks!'
}
});
}]);
```
### **Check form whether valid, return `true` if valid. `checkValid()`**
### **Reset the specific Form. `reset()`**
```html
```
```javascript
// ... checkValid
$scope.form.checkValid = validationProvider.checkValid;
// ... reset
$scope.form.reset = function (form) {
validationProvider.reset(form);
};
// ... angular validation 1.2.0 reset
$scope.form.reset = function () {
// Don't need include validationProvider.reset();
// Focus on your ng-click action
};
```
### **Set the valid/invalid message style CSS**
```html
Your valid message here
Your invalid message here
```
```css
.validation-valid {
}
.validation-invalid {
}
```
### **Custom the valid/invalid message style HTML in `.config()`,**
`setErrorHTML(func)` `setSuccessHTML(func)`, input should be a `function` and given `parameter` which is the valid/invalid message declared
in `getDefaultMsg()`,and finally return the HTML code
```javascript
// your angular
.config(['$validationProvider', function ($validationProvider) {
$validationProvider.setErrorHTML(function (msg, element, attrs) {
// remember to return your HTML
// eg: return '
';
});
}]);
```
### **disable/enable show success/error message**
`default: true`
Easily disable success/error message
```javascript
// your angular
.config(['$validationProvider', function ($validationProvider) {
$validationProvider.showSuccessMessage = false; // or true(default)
$validationProvider.showErrorMessage = false; // or true(default)
}]);
```
### **Multiple validators**
Use commas to separate multiple validators.
```html
```
### **Validator parameters**
The built in `maxlength` and `minlength` validators use parameters to configure the limits. For example:
```html
```
You can use parameters in your custom validators in the same way.
You can access this parameter in the validation expression like so:
```html
```
```javascript
// your module
angular.module('yourApp', ['validation'])
.config(['$validationProvider', function ($validationProvider) {
// Setup `isstring` validation
$validationProvider
.setExpression({
isstring: function (value, scope, element, attrs, param) {
return value === param;
}
})
.setDefaultMsg({
isstring: {
error: 'This is not what we wanted!',
success: 'Thanks!'
}
});
}]);
```
### **Customizable Initial Validation**
Intial Validation for the input false. You can make it to true!
```html
```
### Custom Error/Success Message Function
#### html
Declare your valid and invalid callback functions. Make sure to pass the `message` param.
``` html
```
#### Javascript
Now you can call your own function and have access to the message.
``` javascript
scope.validationValidHandler = function(message){
// you now have access to the error message
displayMessage(message, 'success');
};
scope.validationInvalidHandler = function(message){
// you now have access to the error message
displayMessage(message, 'error');
};
```
### **Setup a global valid/invalid/reset callback in config phase**
```javascript
// your module
angular.module('yourApp', ['validation'])
.config(['$validationProvider', function ($validationProvider) {
validationProvider.validCallback = function(element) {
$(element).parents('.validator-container:first').removeClass('has-error').addClass('has-success-tick');
};
validationProvider.invalidCallback = function(element) {
$(element).parents('.validator-container:first').removeClass('has-success-tick').addClass('has-error');
};
validationProvider.resetCallback = function(element) {
$(element).parents('.validator-container:first').removeClass('has-error');
};
}]);
```
### **Setup Message Element in config phase**
`WHY`
````html
````
`HOW`
In this case, I can use `message-id` attribute as a "get a job done" solution.
Because, If you choose this solution It will increase your effort of manually putting `message-id` and define your own Message Element.
You can use `addMsgElement` as a better solution to centralize & automate your configurations.
```javascript
// your module
angular.module('yourApp', ['validation'])
.config(['$validationProvider', function ($validationProvider) {
/**
* Add your Msg Element
* @param {DOMElement} element - Your input element
* @return void
*/
$validationProvider.addMsgElement = function(element) {
// Insert my own Msg Element
$(element).parent().append('');
};
/**
* Function to help validator get your Msg Element
* @param {DOMElement} element - Your input element
* @return {DOMElement}
*/
$validationProvider.getMsgElement = function(element) {
return $(element).parent().children().last();
};
}]);
```