TinyMCE 和 HTML5 表单验证

发布于 2024-12-22 06:56:10 字数 209 浏览 1 评论 0原文

我在 Rails 应用程序中使用 TinyMCE。在我的表单中,我有一个带有 TinyMCE 的“描述”字段,并且该字段对于表单验证是必需的。

但是,当我尝试提交表单时,由于 HTML5 表单验证的原因,我无法提交表单。我的浏览器(Chrome 和 Firefox)告诉我该字段为空。我还有另一个问题。当 Chrome 显示空字段的对话框时,它出现在我的页面顶部,而不是靠近我的表单字段。

I'm using TinyMCE in a Rails application. In my form, I have a "description" field with TinyMCE and this field is mandatory for the form validation.

But when I try to submit my form, I can't, because of the HTML5 form validation. My browser (Chrome and Firefox) tells me that the field is empty. And I have another problem. When Chrome displays the dialog for the empty field, it appears on the top of my page, not near my form field.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜雨飘雪 2024-12-29 06:56:10

FF 显示所需 fileld 的气泡,但在错误的位置,Chrome 会抛出错误,因为它找不到显示气泡的字段。所以我的解决方案是禁用 TinyMCE 设置的 display:none 样式,并减小字段大小并使其可见隐。这样,浏览器将在该字段旁边显示气泡,因为该字段位于 TinyMCE 编辑器旁边,用户将知道缺少哪些必填字段。

textarea.tinymce {
	background: transparent;
	border: none;
	box-shadow: none;
	display: block !important;
	height: 0;
	resize: none;
	width: 0;
	visibility: hidden;
}

FF show up a bubble for required fileld but at wrong place, Chrome throws an error because it can't find field to show bubble.. So my solution is disable display:none style set by TinyMCE and reduce the field size and make its visibility hidden. this way browser will show bubble next to this field as this field is next to TinyMCE editor user will know what required field is missing.

textarea.tinymce {
	background: transparent;
	border: none;
	box-shadow: none;
	display: block !important;
	height: 0;
	resize: none;
	width: 0;
	visibility: hidden;
}

鸩远一方 2024-12-29 06:56:10

我在文本区域中使用了“oninit”选项并工作:

oninit: function(editor) {
    $currentTextArea.closest('form').bind('submit invalid', function() {
        editor.save();
    });
}

您可以尝试使用 onChange 事件,但它在 Firefox 中无法正常工作。

我的完整代码:

$('textarea.tinymce').each(function(){
    var options = {
        theme : "advanced",
        skin : "cirkuit",
        plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template", theme_advanced_buttons1 :"formatselect,fontsizeselect,forecolor,|,bold,italic,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,spellchecker", 
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true
    },
    $this = $(this);

    // fix TinyMCE bug
    if ($this.is('[required]')) {
        options.oninit = function(editor) {
            $this.closest('form').bind('submit invalid', function() {
                editor.save();
            });
        }
    }
    $this.tinymce(options);
});

I used the "oninit" option in the textareas and worked:

oninit: function(editor) {
    $currentTextArea.closest('form').bind('submit invalid', function() {
        editor.save();
    });
}

You could try to use onChange event, but it doesn't work properly in Firefox.

My complete code:

$('textarea.tinymce').each(function(){
    var options = {
        theme : "advanced",
        skin : "cirkuit",
        plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template", theme_advanced_buttons1 :"formatselect,fontsizeselect,forecolor,|,bold,italic,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,spellchecker", 
        theme_advanced_buttons2 : "",
        theme_advanced_buttons3 : "",
        theme_advanced_buttons4 : "",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true
    },
    $this = $(this);

    // fix TinyMCE bug
    if ($this.is('[required]')) {
        options.oninit = function(editor) {
            $this.closest('form').bind('submit invalid', function() {
                editor.save();
            });
        }
    }
    $this.tinymce(options);
});
青柠芒果 2024-12-29 06:56:10

我拿了 @lucaswxp 代码并对其进行了一些更改,因为 Firefox 抛出了一个错误($this.is 不是一个函数,如果我没记错的话)。
此外,我还将其触发代码的方式从: 更改

 $this.tinymce(options);

为:

tinymce.init(options);

完整代码:

$(window).load(function() {

var options = {
        selector: 'textarea',
        skin: "lightgray"
    },
    $this = $(this);

    // fix tinymce bug
    if ($this.is('[required]')) {
        options.oninit = function(editor) {
            $this.closest('form').bind('submit, invalid', function() {
                editor.save();
            });
        }
    }

    tinymce.init(options);
});

非常感谢创建者,它的工作方式就像奇迹一样:)

I took @lucaswxp code and changed it a bit, because Firefox was throwing an error ($this.is not a function, if I recall it correctly).
Also I changed the way it fires the code from:

 $this.tinymce(options);

to:

tinymce.init(options);

Full code:

$(window).load(function() {

var options = {
        selector: 'textarea',
        skin: "lightgray"
    },
    $this = $(this);

    // fix tinymce bug
    if ($this.is('[required]')) {
        options.oninit = function(editor) {
            $this.closest('form').bind('submit, invalid', function() {
                editor.save();
            });
        }
    }

    tinymce.init(options);
});

Many thanks to the creator, it worked like wonder :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文