jQuery使用Summernote编辑器验证错误:无法读取未定义的属性'replace'

umu*_*sen 12 javascript jquery jquery-validate razor asp.net-mvc-5

我正在使用MVC5使用summernote编辑器构建表单.

剃刀代码:

<div class="form-group">
      @Html.LabelFor(model => model.Content, htmlAttributes: new { @class = "control-label" })
      @Html.EditorFor(model => model.Content, new { htmlAttributes = new { @class = "form-control post-content"} })
      @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

$('#blog-form .post-content').summernote({
  height: 400,                
  minHeight: 300,            
  codemirror: {
    theme: 'default'
  }
});
Run Code Online (Sandbox Code Playgroud)

通过上面的设置,编辑器控件呈现得很好.但是,一旦我离开编辑器,即onBlur,我在控制台中收到以下错误:

Uncaught TypeError: Cannot read property 'replace' of undefined
    at $.validator.escapeCssMeta (jquery.validate.js:1014)
    at $.validator.errorsFor (jquery.validate.js:995)
    at $.validator.prepareElement (jquery.validate.js:679)
    at $.validator.element (jquery.validate.js:466)
    at $.validator.onfocusout (jquery.validate.js:289)
    at HTMLDivElement.delegate (jquery.validate.js:411)
    at HTMLFormElement.dispatch (jquery-2.2.3.js:4737)
    at HTMLFormElement.elemData.handle (jquery-2.2.3.js:4549)
    at Object.trigger (jquery-2.2.3.js:7819)
    at Object.simulate (jquery-2.2.3.js:7890)
Run Code Online (Sandbox Code Playgroud)

这是DOM的渲染部分:

    <div class="form-group">
        <label class="control-label" for="Content">Content</label>
        <textarea class="form-control post-content text-box multi-line" id="Content" name="Content" style="display: none;"></textarea>

        <div class="note-editor note-frame panel panel-default">    
            ...summernote generated...
        </div>

        <span class="field-validation-valid text-danger" data-valmsg-for="Content" data-valmsg-replace="true"></span>
    </div>
Run Code Online (Sandbox Code Playgroud)

jQuery版本:2.2.3

jQuery验证版本:1.16.0

Microsoft.jQuery.Unobtrusive.Validation版本:3.2.3

我做了一些研究,并且已经知道这与summernote插件无关.我尝试在内部和外部文件准备好运行以下代码,但它不起作用:

$.validator.setDefaults({
            ignore: ":hidden:not(.post-content)"
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Jer*_*ook 14

我使用此脚本告诉验证器忽略Summernote元素.可以调整它以忽略其他HTML编辑器生成的元素.

$('form').each(function () {
    if ($(this).data('validator'))
        $(this).data('validator').settings.ignore = ".note-editor *";
});
Run Code Online (Sandbox Code Playgroud)


Bje*_*ego 9

没有硬编码 Id 的最干净的方法来自 JQuery.Validate github 讨论它自己:

jQuery.validator.setDefaults({
  // This will ignore all hidden elements alongside `contenteditable` elements
  // that have no `name` attribute
  ignore: ":hidden, [contenteditable='true']:not([name])"
});
Run Code Online (Sandbox Code Playgroud)

来源:https : //github.com/jquery-validation/jquery-validation/issues/1875


Nic*_*rev 8

如果您在使用 jQuery 验证器验证表单时使用quill(富文本编辑器)遇到此问题,只需执行以下操作:

$('#id-of-form-to-validate').validate({
  ignore: ".ql-container *"
  // continue validation
});
Run Code Online (Sandbox Code Playgroud)

其中 '#id-of-form-to-validate' 可以只是一个 id、类或表单元素。


Kal*_*pit 5

您可以通过稍微调整代码来解决上述错误.

    var v = $('#myForm').validate({ 
        // exclude it from validation
        ignore: ":hidden:not(#summernote),.note-editable.panel-body"
    });

   var myElement = $('#summernote');

   myElement.summernote({
      // See: http://summernote.org/deep-dive/
      callbacks: {
         onChange: function(contents, $editable) {
          // Note that at this point, the value of the `textarea` is not the same as the one
         // you entered into the summernote editor, so you have to set it yourself to make
         // the validation consistent and in sync with the value.
         myElement.val(myElement.summernote('isEmpty') ? "" : contents);

         // You should re-validate your element after change, because the plugin will have
         // no way to know that the value of your `textarea` has been changed if the change
        // was done programmatically.
       v.element(myElement);
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

来源(官方):https://github.com/jquery-validation/jquery-validation/issues/1875#issuecomment-272667183