您好我想知道如何将指令和控制器功能结合起来验证表单?
现在我正在使用指令单独验证表单,如果有人没有填写字段,则突出显示字段紫色,在我的控制器中,我重复相同的验证以查看用户是否可以继续保存数据。
directive.js
.directive('highlightOnError', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
scope.$on('submit', function() {
var border = '';
if (ngModel.$invalid){
border = 'magenta solid 1px';
element.css('border', border);
} else
{
border="";
element.css('border', border);
}
});
}
};
})
controller.js
for(var i=0; i<formobject.length;i++){
var key=formobject[i];
var field =validation.formobject[key];
if(validation.formobject.hasOwnProperty(formobject[i])){
if(field==null){
error_count++;
break;
}
}
else{
error_count++;
break;
}
}
if(error_count==0){
//save data of form
$scope.create();
}
else{
alert('Please fill out highlighted fields');
}
感谢
答案 0 :(得分:3)
虽然这不能直接回答您的问题,但这是一种可能对您有益的替代方法。首先创建一个服务来保存您的验证。
myApp.factory('ValidationService', [ '$log',
function($log) {
var validators = {
passcode: {
patterns: [
{
regex: /^[0-9]{8}$/,
msg: "Please enter a valid 8 digit code."
}
]
},
password: {
patterns: [
{
regex: /^.{8}.*$/,
msg: "Please use at least 8 characters."
},
{
regex: /((?=.*[a-z].*)(?=.*[A-Z].*)(?=.*[0-9].*))|((?=.*[a-z].*)(?=.*[A-Z].*)(?=.*[^a-zA-Z0-9].*))|((?=.*[A-Z].*)(?=.*[0-9].*)(?=.*[^a-zA-Z0-9].*))|((?=.*[a-z].*)(?=.*[0-9].*)(?=.*[^a-zA-Z0-9].*))/,
msg: "Please use characters from 3 different categories."
}
]
},
email: {
patterns: [
{
regex: /^[^@]+@[^.]+[.][^.][^.]+.*$/,
msg: "Please enter a valid email address."
}
]
},
default: {
patterns: [
{
regex: /^.*$/,
msg: "Invalid validator specified. Check your html."
}
]
}
};
return {
getValidator: getValidator
};
function getValidator(inputType) {
if (validators[inputType]) {
return validators[inputType];
}
return validators.default;
}
}]);
然后,使用此服务的指令:
myApp.directive('inputValidator', [ '$log', 'ValidationService',
function($log, ValidationService) {
return {
require: 'ngModel',
link: function(scope, elem, attrs, ctrl) {
ctrl.$parsers.unshift(function(viewValue) {
if (typeof viewValue == 'undefined') {
return viewValue;
}
var pass = true;
var validator = ValidationService.getValidator(attrs.inputValidator);
$.each(validator.patterns, function(index, pattern) {
if (!pattern.regex.test(viewValue)) {
pass = false;
scope[attrs.inputValidatorMsg] = pattern.msg;
return false;
}
});
if (pass) {
// valid
ctrl.$setValidity('inputValidator', true);
return viewValue;
} else {
// invalid
ctrl.$setValidity('inputValidator', false);
return undefined;
}
});
}
};
}]);
最后,有些html将它们捆绑在一起:
<form name="loginForm" class="css-form" data-novalidate>
<p class="reset-header">Activate Your Account</p>
<input type=text placeholder="Email Address" name="username" data-ng-model="username"
data-input-validator="email" data-input-validator-msg="usernameError" data-required>
<span class="tag tag-alert arrow-top" data-ng-show="loginForm.username.$error.inputValidator">{{usernameError}}</span>
<input type=text placeholder="Activation Code" name="passcode" data-ng-model="passcode"
data-input-validator="passcode" data-input-validator-msg="passcodeError" data-required>
<span class="tag tag-alert arrow-top" data-ng-show="loginForm.passcode.$error.inputValidator">{{passcodeError}}</span>
<input type=password placeholder="New Password" name="password" data-ng-model="password"
data-input-validator="password" data-input-validator-msg="passwordError" data-input-must-not-match="username" data-required>
<span class="tag tag-alert arrow-top" data-ng-show="loginForm.password.$error.inputValidator">{{passwordError}}</span>
<span class="tag tag-alert arrow-top" data-ng-show="!loginForm.password.$error.inputValidator && loginForm.password.$error.inputMustNotMatch">Please enter a password that is not your email address.</span>
<input type=password placeholder="New Password Again" name="passwordConfirm" data-ng-model="passwordConfirm"
data-input-must-match="password" data-required>
<span class="tag tag-alert arrow-top" data-ng-show="loginForm.passwordConfirm.$error.inputMustMatch">Please enter the same new password again.</span>
<button class="btn btn-special" data-ng-disabled="loginForm.$invalid" data-ng-click="save()">Activate</button>
</form>