Skip to content

Commit

Permalink
Added support for dynamic default messages using functions. (#198)
Browse files Browse the repository at this point in the history
* Added support for dynamic default messages using functions.

* Replaced validator directive updates lost during conflict resolution.

* Updated ternary based on jshint warning and rebuilt dist

* Added tests for dynamic default messages

* Added dynamic default message documentation
  • Loading branch information
scottadamsmith authored and hueitan committed Mar 9, 2017
1 parent efa301e commit fa1ec0d
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 9 deletions.
23 changes: 23 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ API
<input type="text" name="number" ng-model="form.number" validator="number"/>
```

### **Use Custom Default Valid/Invalid Message** <br/>
You can customize the default valid/invalid message that will be automatically placed next to your input element.

```html
<script>
$validationProvider.setDefaultMsg({
minlength: {
error: function (element, attrs, param) {
return 'A minimum of ' + param + ' characters is required.';
}
}
, maxlength: {
error: function (element, attrs, param) {
return 'A maximum of ' + param + ' characters is required.';
}
}
});
</script>

<label>Username</label>
<input type="text" name="username" ng-model="form.username" validator="minlength=5"/>
```

### **Use a custom Valid Message** <br/>
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`.

Expand Down
12 changes: 8 additions & 4 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ angular.module('validation.directive', ['validation.provider']);
* @param ctrl
* @returns {}
*/
var validFunc = function(element, validMessage, validation, scope, ctrl, attrs) {
var validFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) {
var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).success;
var validCallback = $parse(attrs.validCallback);
var messageId = attrs.messageId;
Expand All @@ -411,6 +411,8 @@ angular.module('validation.directive', ['validation.provider']);
if (element.attr('no-validation-message')) {
messageElem.css('display', 'none');
} else if ($validationProvider.showSuccessMessage && messageToShow) {
messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow;

messageElem.html('').append($compile($validationProvider.getSuccessHTML(messageToShow, element, attrs))(scope));
messageElem.css('display', '');
} else {
Expand All @@ -436,7 +438,7 @@ angular.module('validation.directive', ['validation.provider']);
* @param ctrl
* @returns {}
*/
var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs) {
var invalidFunc = function(element, validMessage, validation, scope, ctrl, attrs, param) {
var messageToShow = validMessage || $validationProvider.getDefaultMsg(validation).error;
var invalidCallback = $parse(attrs.invalidCallback);
var messageId = attrs.messageId;
Expand All @@ -449,6 +451,8 @@ angular.module('validation.directive', ['validation.provider']);
if (element.attr('no-validation-message')) {
messageElem.css('display', 'none');
} else if ($validationProvider.showErrorMessage && messageToShow) {
messageToShow = angular.isFunction(messageToShow) ? messageToShow(element, attrs, param) : messageToShow;

messageElem.html('').append($compile($validationProvider.getErrorHTML(messageToShow, element, attrs))(scope));
messageElem.css('display', '');
} else {
Expand Down Expand Up @@ -555,15 +559,15 @@ angular.module('validation.directive', ['validation.provider']);
var validationGroup = attrs.validationGroup;
var valid = {
success: function(message) {
validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs);
validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs, validatorParam);
if (leftValidation.length) {
return checkValidation(scope, element, attrs, ctrl, leftValidation, value);
} else {
return true;
}
},
error: function(message) {
return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs);
return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs, validatorParam);
}
};

Expand Down
Loading

0 comments on commit fa1ec0d

Please sign in to comment.