Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve valid result, you can set success or error message when valid… #231

Merged
merged 3 commits into from
Aug 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ You can also add a `validation-group` directive to group many elements into a gr

<label>Huei (Custom setup the new validator - Function)</label>
<input type="text" name="huei" ng-model="form.huei" validator="huei"/>

<label>Kuaisheng (Custom setup the new validator - Function)</label>
<input type="text" name="kuaisheng" ng-model="form.kuaisheng" validator="kuaisheng"/>
```

```javascript
Expand Down Expand Up @@ -187,6 +190,37 @@ angular.module('yourApp', ['validation'])
success: 'Thanks!'
}
});

// Setup `kuaisheng` validation
$validationProvider
.setExpression({
kuaisheng: function(value, scope, element, attrs, param) {
var errorStr = [
'errorStr1',
'errorStr2'
];
var len = errorStr.length;
for (var i = len - 1; i >= 0; i--) {
if (value.indexOf(errorStr[i]) > -1) {
return {
result: false,
message: 'input should not include ' + errorStr[i]
};
}
}
return {
result: true,
message: ''
};
}
})
.setDefaultMsg({
kuaisheng: {
error: 'valid is error',
success: 'Thanks!'
}
});

}]);
```

Expand Down
46 changes: 37 additions & 9 deletions dist/angular-validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,32 @@ angular.module('validation.directive', ['validation.provider']);
*/
var focusElements = {};

/**
* Get Validation Result Object
* @param data
* @returns {
* result: Boolean, // is success or error
* message: String // tips
* }
*/
function getResultObj(data) {
var res = {};
if (data && data.length > 0) {
res = data[0];
if (!angular.isObject(res)) {
res = {
result: res,
message: ''
};
}
} else {
res = {
result: false,
message: ''
};
}
return res;
}

/**
* Check Validation with Function or RegExp
Expand All @@ -528,16 +554,16 @@ angular.module('validation.directive', ['validation.provider']);
var expression = $validationProvider.getExpression(validator);
var validationGroup = attrs.validationGroup;
var valid = {
success: function() {
validFunc(element, attrs[successMessage], validator, scope, ctrl, attrs);
success: function(message) {
validFunc(element, message || attrs[successMessage], validator, scope, ctrl, attrs);
if (leftValidation.length) {
return checkValidation(scope, element, attrs, ctrl, leftValidation, value);
} else {
return true;
}
},
error: function() {
return invalidFunc(element, attrs[errorMessage], validator, scope, ctrl, attrs);
error: function(message) {
return invalidFunc(element, message || attrs[errorMessage], validator, scope, ctrl, attrs);
}
};

Expand All @@ -550,12 +576,14 @@ angular.module('validation.directive', ['validation.provider']);
if (expression.constructor === Function) {
return $q.all([$validationProvider.getExpression(validator)(value, scope, element, attrs, validatorParam)])
.then(function(data) {
if (data && data.length > 0 && data[0]) {
var resultObj = getResultObj(data);
var message = resultObj.message;
if (resultObj.result) {
if (validationGroup) {
groups[validationGroup][ctrl.$name] = true;
setValidationGroup(scope, validationGroup, true);
}
return valid.success();
return valid.success(message);
} else if (validationGroup) {
groups[validationGroup][ctrl.$name] = false;

Expand All @@ -565,9 +593,9 @@ angular.module('validation.directive', ['validation.provider']);
setValidationGroup(scope, validationGroup, true);
} else {
setValidationGroup(scope, validationGroup, false);
return valid.error();
return valid.error(message);
}
} else return valid.error();
} else return valid.error(message);
}, function() {
return valid.error();
});
Expand Down Expand Up @@ -647,7 +675,7 @@ angular.module('validation.directive', ['validation.provider']);
var uid = ctrl.validationId = guid();

/**
* to have avalue to rollback to
* to have a value to rollback to
*/
var originalViewValue = null;

Expand Down
Loading