内容简介:翻译自:https://stackoverflow.com/questions/42379637/how-to-use-the-angular-jquery-validates-checkform-function
编辑:
I’ve added a JsFiddle 因此您可以轻松排除故障,而不必自己设置环境.如您所见,即使在输入元素上的blur事件之前,也会在“电子邮件”字段上进行验证,这是由$scope.Email更改触发的.如果您在<p>上注释掉ng-show =“!mainForm.validate()”元素,你会发现问题没有发生.
我正在使用 Angular implementation of jQuery Validate ,我需要能够检查表单是否有效而不显示错误消息.我在网上看到的标准解决方案是使用jQuery Validate的checkForm()函数,如下所示:
$('#myform').validate().checkForm()
但是,我正在使用的Angular包装器当前没有实现checkForm函数.我一直在尝试修改源代码以引入它,我担心我会在脑海中.代码很小很简单,我会在这里粘贴它:
(function (angular, $) {
angular.module('ngValidate', [])
.directive('ngValidate', function () {
return {
require: 'form',
restrict: 'A',
scope: {
ngValidate: '='
},
link: function (scope, element, attrs, form) {
var validator = element.validate(scope.ngValidate);
form.validate = function (options) {
var oldSettings = validator.settings;
validator.settings = $.extend(true, {}, validator.settings, options);
var valid = validator.form();
validator.settings = oldSettings; // Reset to old settings
return valid;
};
form.numberOfInvalids = function () {
return validator.numberOfInvalids();
};
//This is the part I've tried adding in.
//It runs, but still shows error messages when executed.
//form.checkForm = function() {
// return validator.checkForm();
//}
}
};
})
.provider('$validator', function () {
$.validator.setDefaults({
onsubmit: false // to prevent validating twice
});
return {
setDefaults: $.validator.setDefaults,
addMethod: $.validator.addMethod,
setDefaultMessages: function (messages) {
angular.extend($.validator.messages, messages);
},
format: $.validator.format,
$get: function () {
return {};
}
};
});
}(angular, jQuery));
我希望能够用它来显示或隐藏消息,如下所示:
<p class="alert alert-danger" ng-show="!mainForm.checkForm()">Please correct any errors above before saving.</p>
我不仅仅使用!mainForm.validate()的原因是因为这会导致错误消息在元素被“模糊”之前显示在元素上,这正是我想要避免的.谁能帮助我在这个angular指令中实现checkForm()函数?
(function (angular, $) {
angular.module('ngValidate', [])
.directive('ngValidate', function () {
return {
require: 'form',
restrict: 'A',
scope: {
ngValidate: '='
},
link: function (scope, element, attrs, form) {
var validator = element.validate(scope.ngValidate);
form.validate = function (options) {
var oldSettings = validator.settings;
validator.settings = $.extend(true, {}, validator.settings, options);
var valid = validator.form();
validator.settings = oldSettings; // Reset to old settings
return valid;
};
form.checkForm = function (options) {
var oldSettings = validator.settings;
validator.settings = $.extend(true, {}, validator.settings, options);
var valid = validator.checkForm();
validator.submitted = {};
validator.settings = oldSettings; // Reset to old settings
return valid;
};
form.numberOfInvalids = function () {
return validator.numberOfInvalids();
};
}
};
})
.provider('$validator', function () {
$.validator.setDefaults({
onsubmit: false // to prevent validating twice
});
return {
setDefaults: $.validator.setDefaults,
addMethod: $.validator.addMethod,
setDefaultMessages: function (messages) {
angular.extend($.validator.messages, messages);
},
format: $.validator.format,
$get: function () {
return {};
}
};
});
}(angular, jQuery));
请在这里找到更新的jsFiddle https://jsfiddle.net/b2k4p3aw/
参考: Jquery Validation: Call Valid without displaying errors?
翻译自:https://stackoverflow.com/questions/42379637/how-to-use-the-angular-jquery-validates-checkform-function
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Go中函数、匿名函数和递归的使用
- Java 8函数式编程模式:不要使用匿名函数
- 015.Python函数名的使用以及函数变量的操作
- c++中transform()函数和find()函数的使用方法。
- Axure函数使用手册
- 高阶函数的使用
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Numerical Methods and Methods of Approximation in Science and En
Karan Surana / CRC Press / 2018-10-31
ABOUT THIS BOOK Numerical Methods and Methods of Approximation in Science and Engineering prepares students and other readers for advanced studies involving applied numerical and computational anal......一起来看看 《Numerical Methods and Methods of Approximation in Science and En》 这本书的介绍吧!