我在表单中有许多不同的部分,并且每个部分的输入必须等于要验证的表单的第一个输入(在其自己的部分中)。
验证工作正常,唯一的问题是,当用户尝试验证某节中的图形不正确时,UI会在该节的其余部分显示错误。在这种情况下,该部分的每个输入上都会显示错误。这很棒。但是,当用户更改其中一个字段中的数字并且总数现在正确时,欧芹仅清除该特定字段中的错误。
这是可以正常工作的代码,但不能清除错误:
window.ParsleyValidator
.addValidator('sumTotal', function (value, group) {
// get total enrollment number
var totalInput = $('#enrollment-group').find('[data-parsley-sum-total="all"]')
var totalEnrollment = parseInt(totalInput.val())
// sum up input values for the section
var sectionSum = 0;
$('#enrollment-group').find('[data-parsley-sum-total="' + group + '"]').each(function(){
if ($(this).val()) sectionSum += parseInt($(this).val());
})
// if no inputs, don't consider the section
if (sectionSum === 0) return true
return totalEnrollment === sectionSum;
}, 32)
.addMessage('en', 'sumTotal', 'The total sum for all %s fields must equal the total enrollment above: ' + $('#enrollment-group').find('[data-parsley-sum-total="all"]').val());
// exclude the 'total enrollment' input from the sum validation
$('form').parsley({ excluded: '[data-parsley-sum-total="all"]' });
这是我尝试清除错误但最终以无限递归结束的代码示例:
$('form').parsley().subscribe('parsley:field:success', function(input){
$.each($('[data-parsley-sum-total]'), function(i, value){
if ($(value).hasClass('parsley-error')){
$(value).parsley().validate()
}
})
});
因此,该部分有效后,如何清除该部分输入中的所有错误?
这是在django管理模板中,因此我必须在parsley.js加载后添加我的自定义验证器。在此先感谢您的帮助。
好吧,经过一番摆弄之后,我发现了它,这里是给有类似问题的任何人的:
window.ParsleyValidator
.addValidator('sumTotal', function (value, group) {
// get total enrollment number
var totalInput = $('#enrollment-group').find('[data-parsley-sum-total="all"]')
var totalEnrollment = parseInt(totalInput.val())
// sum up input values for the section
var sectionSum = 0;
$('#enrollment-group').find('[data-parsley-sum-total="' + group + '"]').each(function(){
if ($(this).val()) sectionSum += parseInt($(this).val());
})
// if no inputs, don't consider the section
if (sectionSum === 0) return true
return totalEnrollment === sectionSum;
}, 32)
.addMessage('en', 'sumTotal', 'The total sum for all %s fields must equal the total enrollment above: ' + $('#enrollment-group').find('[data-parsley-sum-total="all"]').val());
// exclude the 'total enrollment' input from the sum validation
$('form').parsley({ excluded: '[data-parsley-sum-total="all"]' });
// clear other errors in section if one field emits success event
$('form').parsley().subscribe('parsley:field:success', function(input){
$.each($('[data-parsley-sum-total]'), function(i, value){
if ($(value).attr('data-parsley-sum-total') == input.options.sumTotal){
window.ParsleyUI.removeError($(value).parsley(), 'sumTotal')
$(value).removeClass('parsley-error')
$(value).addClass('parsley-success')
}
})
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句