gpt4 book ai didi

jquery - 清除客户端验证 Rails gem 中的错误消息

转载 作者:行者123 更新时间:2023-12-01 05:00:41 25 4
gpt4 key购买 nike

我在我的应用程序中使用客户端验证rails gem v3.1.0。我在 jquery 对话框中显示了一个需要验证的表单。该表单上启用了客户端状态验证。验证一切正常,但是当用户取消对话框(不提交)时,我想清除任何现有的验证错误。这样,当用户再次触发对话框时,表单上就不会出现验证错误。

我尝试通过在对话框关闭时触发每个元素的客户端状态验证传递事件来实现此目的 - 它通过删除包装错误字段和标签来实现。一个问题是,在此代码之后后续验证不起作用。我觉得绑定(bind)的事件被删除并且不再触发。

是否有更好的方法来实现这一目标?

我的 jquery 对话框:

$('#create_dialog').dialog({
height: 260,
width: 420,
title: "title",
buttons: {"<%= I18n.t("create") %>": function(e) {
$('#new_segment').submit();
},
Cancel: function() {
var form = $('#new_segment');
form.find('[data-validate]:input').each(function() {
$(this).trigger('element:validate:pass');
});
$(this).dialog("close");

}
}
});

最佳答案

这不是一个干净的方法,但是可行

/**
* Remove Embedded validation perfectly that is compatible with validation gem
* @param field
* @returns {boolean}
*/
function removeValidationMessage(field) {
var $field = $(field) || field;
/**
* First wrap the previously occurring label in wrapper div with class 'field_with_errors'
* Then Wrap the field within a wrapper div of same class
* Then add validation message after the field with class name 'message'
*/
if ($field.siblings('.message').length > 0) {
$field.parent().prev('.field_with_errors').find('label').unwrap();
$field.unwrap();
$field.next('.message').remove();
return true;
}
return false;
}

关于jquery - 清除客户端验证 Rails gem 中的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10466132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com