实施指令以从验证中排除隐藏的输入元素($addControl 问题) [英] implementing a directive to exclude a hidden input element from validation ($addControl issue)
问题描述
我正在创建一个指令以从验证中排除隐藏的输入元素:http://plnkr.co/edit/Vnwvq2AT7JpgTnoQwGh9?p=preview
I'm creating a directive to exclude a hidden input element from validation: http://plnkr.co/edit/Vnwvq2AT7JpgTnoQwGh9?p=preview
app.directive('shownValidation', function() {
return {
require: '^form',
restrict: 'A',
link: function(scope, element, attrs,form) {
var control;
scope.$watch(attrs.ngShow,function(value){
if (!control){
control = form[element.attr("name")];
}
if (value == true){
form.$addControl(control);
}else{
form.$removeControl(control);
}
});
}
};
});
这里的想法是,如果元素被隐藏,我会从Form中移除控件,这样它就不会影响其他输入的有效性.当我调用 form.$removeControl(control);
时它工作正常,您可以在演示中通过删除名字并单击按钮隐藏字段来测试它.
The idea here is if the element is hidden, I will remove the control from the Form so that it will not affect the other input validity. It works fine when I call form.$removeControl(control);
, you can test that in the demo by removing the firstname and clicking on the button to hide the field.
但是当我再次单击按钮时,即使名字无效(空),表单有效性仍然是真实的
But when I click the button again, the form validity is still true even though the firstname is invalid (empty)
我还尝试添加 form.$setValidity(form.$valid && control.$valid)
=> 有效性状态按预期更新,但是当我输入名字时,有效性仍然是错误的.
I also tried adding form.$setValidity(form.$valid && control.$valid)
=> the validity state is updated as expected but when I type into the firstname, the validity is still false.
我的问题是如何解决这个问题?感谢您的回复.
My question is how to solve this problem? Thanks for any replies.
更新:
感谢@Michael 的回答.这是有效的解决方案:
Thanks to @Michael's answer. Here is the working solution:
app.directive('shownValidation', function() {
return {
require: '^form',
restrict: 'A',
link: function(scope, element, attrs, form) {
var control;
scope.$watch(attrs.ngShow, function(value) {
if (!control) {
control = form[element.attr("name")];
}
if (value == true) {
form.$addControl(control);
//Add a forEach to manually update form validity.Thanks to @Michael's answer
angular.forEach(control.$error, function(validity, validationToken) {
form.$setValidity(validationToken, !validity, control);
});
} else {
form.$removeControl(control);
}
});
}
};
});
推荐答案
如果控件被移除 angular 更新有效性(来自来源):
If the control is removed angular updates the validity (from the sources):
form.$removeControl = function(control) {
if (control.$name && form[control.$name] === control) {
delete form[control.$name];
}
forEach(errors, function(queue, validationToken) {
form.$setValidity(validationToken, true, control);
});
arrayRemove(controls, control);
};
如果添加控制angular没有更新有效性(来自sources):
If you add the control angular did not update the validity (from the sources):
form.$addControl = function(control) {
// Breaking change - before, inputs whose name was "hasOwnProperty" were quietly ignored
// and not added to the scope. Now we throw an error.
assertNotHasOwnProperty(control.$name, 'input');
controls.push(control);
if (control.$name) {
form[control.$name] = control;
}
};
所以我们必须手动执行此操作.我猜是这样:
so we have to do this manually. I guess this way:
if (value == true){
form.$addControl(control);
angular.forEach(control.$error, function(validity, validationToken) {
form.$setValidity(validationToken, !validity, control);
});
}else{
form.$removeControl(control);
}
}
这篇关于实施指令以从验证中排除隐藏的输入元素($addControl 问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!