summernote与validation

版本

  • 页面框架 bootstrap4
  • summernote v0.8.16
  • jquery-validation v1.19.1
    summernote与validation_第1张图片

封装

sumernotex.js

//ignore:".note-editor *",

function editSummerNote(editorName) {
    validatorSummerNote();
    summerNote(editorName);
}
function initSummerNote(editorName) {
    validatorSummerNote();
    summerNote(editorName);
    $(editorName).summernote("code","");
}

function setSummerNote(editorName,codeHtml) {
    $(editorName).summernote("code",codeHtml == "" || codeHtml == "


"?"":codeHtml); } function validatorSummerNote() { $.validator.addMethod("summernote", function (value, element) { var html = $(element).summernote('code'); return html != "" || html == "


"; }, "富文本不能为空"); } function summerNote(editorName) { //初始化富文本框https://github.com/jquery-validation/jquery-validation/issues/2212 $(editorName).summernote({ toolbar: [['style', ['style']], ['font', ['bold', 'underline', 'clear']], ['fontname', ['fontname']], ['color', ['color']], ['para', ['ul', 'ol', 'paragraph']], ['table', ['table']], ['insert', ['link', 'picture']], ['view', ['fullscreen']]], lang: "zh-CN", height: 200, minHeight: 200, //focus: true, callbacks: { onImageUpload: function(files) { uploadSummerPic(files[0], editorName); } } }); } function uploadSummerPic(file, editorName) { var fd = new FormData(); fd.append("file", file); $.ajax({ type: "POST", url: "/attachment/uploads", data: fd, cache: false, contentType: false, processData: false, success: function (result) { if (result.success == true) { var size = result.data.length; if (size > 0) { $(editorName).summernote('insertImage',result.data[0]); } else { toastr.error("上传文件失败"); } } else { toastr.error("上传文件失败"); } } }); }

定制表单验证,这个的看你页面的布局,一般bootstrap4表单例子就够了,其它看着微调吧

/**
 * 表单验证
 * @param $form
 * @param options
 */
function formValidate($form, options) {
    $($form).validate($.extend(true, {
            errorElement: "div",
            errorClass: "invalid-feedback",
            validClass: "",
            errorPlacement: function (error, element) {
                // Add the `invalid-feedback` class to the error element
                if ($(element).prop("type") === "textarea") {
                    error.appendTo(element.parent());
                } else if ($(element).prop("type") === "select-multiple") {
                    error.appendTo(element.parent().parent());
                } else if ($(element).prop("type") === "select-one") {
                    error.appendTo(element.parent().parent());
                } else if ($(element).prop("type") === "checkbox") {
                    error.appendTo(element.parent().parent());
                } else if ($(element).prop("type") === "radio") {
                    error.appendTo(element.parent().parent());
                } else if ($(element).prop("type") === "file") {
                    error.appendTo(element.parent().parent().parent().parent().parent());
                } else {
                    error.appendTo(element.parent());
                }
            },
            highlight: function (element, errorClass, validClass) {
                if ($(element).prop("type") === "textarea") {
                    $(element).addClass("is-invalid").removeClass("is-valid");
                } else if ($(element).prop("type") === "select-multiple") {
                    $(element).parent().addClass("is-invalid").removeClass("is-valid");
                } else if ($(element).prop("type") === "select-one") {
                    $(element).parent().addClass("is-invalid").removeClass("is-valid");
                } else if ($(element).prop("type") === "radio") {
                    $(element).parent().parent().children().filter(".custom-radio").children("input").addClass("is-invalid").removeClass("is-valid");
                } else if ($(element).prop("type") === "file") {
                    $(element).parent().parent().parent().parent().addClass("is-invalid").removeClass("is-valid");
                } else {
                    $(element).addClass("is-invalid").removeClass("is-valid");
                }
            },
            unhighlight: function (element, errorClass, validClass) {
                if ($(element).prop("type") === "textarea") {
                    $(element).addClass("is-valid").removeClass("is-invalid");
                } else if ($(element).prop("type") === "select-one") {
                    $(element).parent().addClass("is-valid").removeClass("is-invalid");
                } else if ($(element).attr("type") === "radio") {
                    $(element).parent().parent().children().filter(".custom-radio").children("input").addClass("is-valid").removeClass("is-invalid");
                } else if ($(element).prop("type") === "file") {
                    $(element).parent().parent().parent().parent().addClass("is-valid").removeClass("is-invalid");
                } else {
                    $(element).addClass("is-valid").removeClass("is-invalid");
                }
            }
        }, options)
    );
}

使用

-  initSummerNote("#textarea_id");
-  formValidate($('#biaodanForm'), {
        ignore:".note-editor *",
        rules: {
            textarea_name: {
                summernote: true
            }
        },
        messages: {
            description: {
                summernote: "笔记描述不能为空"
            }
        }
    });
 -  保存时候这样校验
    if (!$('#biaodanForm').valid()) {
        return;
    }
    。。。。。。

你可能感兴趣的:(summernote与validation)