简体   繁体   English

Trumboqyg 所见即所得编辑器不适用于 Jquery.Validate 插件,“Uncaught TypeError: Cannot read property 'nodeName' of undefined”

[英]Trumboqyg WYSIWYG editor does not work with Jquery.Validate plugin, "Uncaught TypeError: Cannot read property 'nodeName' of undefined"

I have been pulling my hair out trying to figure this out.我一直在努力解决这个问题。

I am running jquery 2.2.2 at the moment with the jquery validation plugin.我现在正在运行 jquery 2.2.2 和 jquery 验证插件。 https://jqueryvalidation.org/ And Trumbowyg WYSIWYG Editor. https://jqueryvalidation.org/和 Trumbowyg 所见即所得编辑器。 https://alex-d.github.io/Trumbowyg/ https://alex-d.github.io/Trumbowyg/

It's great mostly everything works however, every time a use clicks or does anything really.这很棒,但大多数情况下一切正常,每次使用点击或真正做任何事情时。 You get the console error Uncaught TypeError: Cannot read property 'nodeName' of undefined .您收到控制台错误Uncaught TypeError: Cannot read property 'nodeName' of undefined It also creates certain parts of Trumbowyg to not work properly.它还会导致 Trumbowyg 的某些部分无法正常工作。

So my solution was to use the ignore class on the jquery validation plugin.所以我的解决方案是在 jquery 验证插件上使用忽略 class。 But it does not work.但它不起作用。 I have tried ignoring almost every class labeling for different fields and no matter what I do it is never ignored.我已经尝试忽略几乎每个 class 不同领域的标签,无论我做什么,它都不会被忽略。

So how do I use the jquery validation plugin, with the trumbowyg text editor?那么如何使用 jquery 验证插件和 trumbowyg 文本编辑器呢?

Here is my validation code.这是我的验证码。

$('form').validate({
    errorPlacement: function (error, element) {
        if ($(element).parent('.input-group').length) {
            error.insertAfter(element.parent().siblings('label'));
            error.addClass('alert alert-danger validate');
        } else {
            error.insertAfter(element.siblings('label'));
            error.addClass('alert alert-danger validate');
        }
    },
    errorElement: 'div',
    highlight: function (element) {
        $(element).parent().addClass("has-error");
    },
    unhighlight: function (element) {
        $(element).parent().removeClass("has-error");
    },
    ignore: '.advanced',
    rules: {
        password_confirm: {
            equalTo: 'input[name=password]'
        },
        user_username: {
            remote: {
                url: 'action/check/',
                type: 'POST',
                data: {
                    id: function () {
                        return $('input[name=id]').val();
                    }
                }
            }
        },
        nav_name: {
            remote: {
                url: 'action/check/',
                type: 'POST',
                data: {
                    id: function () {
                        return $('input[name=id]').val();
                    }
                }
            }
        }
    },
    messages: {
        password_confirm: {
            equalTo: "Passwords must match"
        },
        user_username: {
            remote: "This Username already exists"
        },
        nav_name: {
            remote: "This Nav Item Name already exists"
        }
    },
    submitHandler: function (form) {
        $('button[type="submit"]').addClass('disabled').prop('disabled', true).prepend('<span class="bootstrap-dialog-button-icon glyphicon glyphicon-asterisk icon-spin"></span>');
        form.submit();
    }
});

I did remove the validation plugin and everything with trumbowyg did work.我确实删除了验证插件,并且 trumbowyg 的所有内容都可以正常工作。 So maybe it's time to find a new validation plugin, I really would like to avoid that.所以也许是时候找到一个新的验证插件了,我真的很想避免这种情况。

Update: I did notice that firefox is giving me a different error, so I figured I would share that as well.更新:我确实注意到 firefox 给我一个不同的错误,所以我想我也会分享它。

TypeError: owner is undefined
owner[ this.expando ] && owner[ this.expando ][ key ];

An example of the problem should be available to demo here: http://develop.chrischampeau.com/admin/test.php该问题的示例应可在此处进行演示: http://develop.chrischampeau.com/admin/test.php

This is a known issue with the jQuery Validation plugin where content editable elements in forms would cause exceptions when an event is triggered. 这是jQuery Validation插件的一个已知问题,其中触发事件时,表单中内容可编辑的元素会导致异常。

You can find this issue describing the exact same problems: 你可以找到这个问题的描述完全一样的问题:

  • Breaks your WYSIWYG. 打破您的所见即所得。
  • Undefined errors. 未定义的错误。
  • All toolbar buttons in your WYSIWYG take two clicks to trigger. 所见即所得中的所有工具栏按钮都单击两次即可触发。

A modification / hack is proposed in this thread to resolve the issue but a pull request fixing this same issue has already been merged into the project in May. 在该线程中提出了修改/修改来解决该问题,但是解决同一问题的请求请求已于5月合并到项目中。

The problem is that the latest release is from February so this merge is not available at the moment. 问题在于最新版本是从2月开始的,因此此合并暂时不可用。 According to this other issue , a maintainer stated 15 hours ago that a release is coming very soon and it includes the fix for your problem. 根据另一个问题 ,维护者在15个小时前表示即将发布一个版本,其中包含针对您的问题的修复程序

If you can't wait for the new release, you can always get the latest version from their repository or cherry pick yourself the fix included in this pull request . 如果您迫不及待想要发布新版本,则始终可以从其存储库中获取最新版本,或者自行挑选此pull request中包含的修复程序。

Regarding the second issue you described in a comment after applying the fix from the pull request, jQuery Validation check on various events ( onfocusout especially in this case) if an element should be ignored or not by checking if it has a class specified in the settings. 关于您在应用拉取请求中的修复后在注释中描述的第二个问题, jQuery验证会检查各种事件(尤其是onfocusout )在各种事件中是否应该忽略元素,方法是检查元素是否在设置中指定了类。

You're adding to your textarea class="advanced" to ignore it, unfortunately, when Trumbowyg is initialized, the wrapper created will be the following: 不幸的是,您要添加到textarea class="advanced"以忽略它,不幸的是,在初始化Trumbowyg时,创建的包装器将如下所示:

<div class="trumbowyg-editor trumbowyg-reset-css" contenteditable="true" dir="ltr" placeholder="">

It does not propagates the advanced class to the wrapper. 它不会将advanced类传播到包装器。 A potential fix is to add yourself this class when Trumbowyg is initialized. 一种可能的解决方法是在初始化Trumbowyg时添加您自己的此类。 To do that, you can rely on the tbwinit event by modifying your constructor. 为此,您可以通过修改构造函数来依赖tbwinit事件。

// Setup WYSIWYG
$('textarea.advanced').trumbowyg( {
    resetCss: true,
    removeformatPasted: true,
    autogrow: true,
    btnsDef: {
    // Customizables dropdowns
        image: {
            dropdown: ['insertImage', 'upload', 'base64', 'noEmbed'],
            ico: 'insertImage'
        }
    },
    btns: [
        ['viewHTML'],
        ['undo', 'redo'],
        ['formatting'],
        'btnGrp-design',
        ['link'],
        ['image'],
        'btnGrp-justify',
        'btnGrp-lists',
        ['foreColor', 'backColor'],
        ['preformatted'],
        ['horizontalRule'],
        ['fullscreen']
    ],
    plugins: {
        // Add imagur parameters to upload plugin
        upload: {
            serverPath: 'https://api.imgur.com/3/image',
            fileFieldName: 'image',
            headers: {
                'Authorization': 'Client-ID 9e57cb1c4791cea'
            },
            urlPropertyName: 'data.link'
        }
    }
}).on('tbwinit', function(){
  $('.trumbowyg-editor').addClass('advanced');
});

After initialization, your wrapper will have the advanced class so that jQuery Validation can use it. 初始化之后,您的包装器将具有advanced类,以便jQuery Validation可以使用它。

<div class="trumbowyg-editor trumbowyg-reset-css advanced" contenteditable="true" dir="ltr" placeholder="">

Regarding the second issue, it's triggered because some libraries like Trumbowyg when they open a popup (like the ones your mentioned in your comment: Insert Link , Insert Image , etc.), they are actually appending a <form> in the DOM but inside another <form> which leads to nested forms. 关于第二个问题,它的触发是因为某些库(例如Trumbowyg)在打开弹出窗口时(例如您在注释中提到的库: Insert LinkInsert Image等),实际上是在DOM中但在内部添加了<form>另一个<form>导致嵌套的表单。

In the case of nested forms, the second error you mentioned is triggered by jQuery Validation. 对于嵌套表单,您提到的第二个错误是由jQuery Validation触发的。 You can check the comment of the jQuery Validation plugin owner regarding this problem on this message : 您可以在此消息上检查jQuery Validation插件所有者关于此问题的评论:

Nesting forms is invalid HTML. 嵌套表单是无效的HTML。 Either the [...] plugin doesn't generate a form if there is already one, or it has to append it outside the current form. daccess-ods.un.org daccess-ods.un.org如果已经有一个插件,它不会生成一个表单,或者必须将其追加到当前表单之外。

There is already an issue opened on the Trumbowyg Github regarding this problem, but it's a large modification on the core of the plugin and at this moment, it's moved from milestone to milestone since 2015. Trumbowyg Github上已经存在一个与此问题有关的问题,但是它是对该插件核心的重大修改,目前,自2015年以来,它已从里程碑转移到里程碑。

If you're still getting the following error on line jquery.validate.min.js:formatted:490 whenever the jQuery validation tries to kick in on a Trumbowyg element:如果每当 jQuery 验证尝试启动 Trumbowyg 元素时,您仍然在jquery.validate.min.js:formatted:490行收到以下错误:

Uncaught TypeError: Cannot read properties of undefined (reading 'replace')

Try adding a random value to an id attribute in the trumbowyg-editor div.尝试向trumbowyg-editor div 中的id属性添加一个随机值。 Something like this will work:这样的事情会起作用:

$(".trumbowyg").find(".trumbowyg-editor").attr("id", Math.random());

This may not kick start the actual validation, but it will get rid of the error.这可能不会启动实际验证,但会消除错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery验证未捕获的TypeError无法读取未定义的属性“ form” - jQuery validate Uncaught TypeError Cannot read property 'form' of undefined 未捕获的类型错误:无法读取 null 的属性“nodeName” - Uncaught TypeError: Cannot read property 'nodeName' of null 未捕获的类型错误:无法读取未定义的属性“createDocumentFragment”(使用所见即所得) - Uncaught TypeError: Cannot read property 'createDocumentFragment' of undefined (using wysiwyg) TypeError:无法读取未定义的属性“ nodeName” - TypeError: Cannot read property 'nodeName' of undefined VideoJS 错误 Uncaught TypeError: Cannot read property 'nodeName' of null - VideoJS error Uncaught TypeError: Cannot read property 'nodeName' of null bs-tooltip TypeError:无法读取未定义的属性“nodeName” - bs-tooltip TypeError: Cannot read property 'nodeName' of undefined JQuery 自动完成:未捕获的类型错误:无法读取未定义的属性“值” - JQuery autocomplete: Uncaught TypeError: Cannot read property 'value' of undefined 未捕获的TypeError:无法读取未定义的JQUERY自动完成的属性&#39;length&#39; - Uncaught TypeError: Cannot read property 'length' of undefined JQUERY autocomplete jQuery UI:未捕获的TypeError:无法读取未定义的属性“display” - jQuery UI: Uncaught TypeError: Cannot read property 'display' of undefined Javascript和Jquery:未捕获的TypeError:无法读取未定义的属性&#39;length&#39; - Javascript and Jquery: Uncaught TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM