清除客户端验证中的错误消息 轨道 gem



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

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

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

我的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;
}

https://github.com/bcardarella/client_side_validations/issues/328

来自 Brian Cardarella,客户端验证 gem 的开发人员:

不幸的是,现在没有一种干净的方法来做到这一点。这将在 4.0 中发生变化

最新更新