JQuery Validation using Summernote Editor error: cannot read replace property from undefined

I am using MVC5 to create a form with the summernote editor.

Shaver Code:

<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>

JS:

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

With the above setting, the editor editor displays fine. However, as soon as I get away from the editor, i.e. onBlur, I get the following error in the console:

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)

Here is the render part of the 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>

jQuery version: 2.2.3

jQuery Check Version: 1.16.0

Microsoft.jQuery.Unobtrusive.Validation version: 3.2.3

I have done some research and already know that this is not related to the summernote plugin. I tried to run the following code inside and outside the document, but it did not work:

$.validator.setDefaults({
            ignore: ":hidden:not(.post-content)"
});

Any ideas?

+12
7

script, Summernote. , , HTML.

$('form').each(function () {
    if ($(this).data('validator'))
        $(this).data('validator').settings.ignore = ".note-editor *";
});
+13

, .

    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);
    }
  }
});

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

+5

Quill ( ), :

<div id="quillEditor"></div>

var editorForm = $('.rich-text-editor').closest('form');
if (editorForm.data('validator')) {
    // tell jQuery Validate to ignore all child elements of quill
    editorForm.data('validator').settings.ignore = ".ql-container *";
}
+1

- quill ( ) jQuery, :

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

"# id-of-form-to-validate" id, class form.

+1

jQuery TE, , .

$('#id-of-form-to-validate').validate({
    ignore: ".jqte *"
    // continue with validation
});
0

, . :

jQuery.validator.setDefaults({ ignore: ":hidden:not(#summernote),.note-editable.panel-body"});
0

- 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])"
});

Souce: https://github.com/jquery-validation/jquery-validation/issues/1875

0
source

Source: https://habr.com/ru/post/1669025/


All Articles