How to make textarea filed mandatory when I've applied TinyMCE

試著忘記壹切 提交于 2021-02-15 05:35:40

问题


I want to make textarea filed mandatory when I've applied TinyMCE.

If I add required attribute to <textarea>, it causes that I cannot submit the form even if I fill in the form!

How can I solve this problem?

tinymce.init({
    selector: '#summaryId',
    max_chars: 255, // max. allowed chars
    plugins: "paste",
    setup: function (ed) {
        var allowedKeys = [8, 37, 38, 39, 40, 46]; // backspace, delete and cursor keys
        ed.on('keydown', function (e) {
            if (allowedKeys.indexOf(e.keyCode) != -1) return true;
            if (tinymce_getContentLength() + 1 > this.settings.max_chars) {
                e.preventDefault();
                e.stopPropagation();
                return false;
            }
            return true;
        });
        ed.on('keyup', function (e) {
            tinymce_updateCharCounter(this, tinymce_getContentLength());
        });
    },
    init_instance_callback: function () { // initialize counter div
        $('#' + this.id).prev().append('<div class="char_count" style="text-align:left; color:grey;"></div>');
        tinymce_updateCharCounter(this, tinymce_getContentLength());
    },
    paste_preprocess: function (plugin, args) {
        var editor = tinymce.get(tinymce.activeEditor.id);
        var len = editor.contentDocument.body.innerHTML.length;
        var textLen = args.content.length;// $(args.content).text();
        if (len + textLen > editor.settings.max_chars) {
            alert('Pasting this exceeds the maximum allowed number of ' + editor.settings.max_chars + ' characters.');
            args.content = '';
        } else {
            //tinymce_updateCharCounter(editor, len + text.length);
        }
    }
});

in cshtml file:

<textarea asp-for="Article.Summary" class="form-control" id="summaryId" maxlength="255"></textarea>

回答1:


There are two issues you will have to address here...

1 - The required attribute on the <textarea>

When you invoke TinyMCE the original <textarea> you have on the page is hidden and TinyMCE places a chunk of HTML onto the page. The editing rectangle is an <iframe> and the menus/toolbars are HTML that surrounds that <iframe>.

Because the original <textarea> is now hidden, making it required is an issue and the browser will complain that you have a hidden form element marked as required.

Bottom line for this first issue is you can't mark a hidden <textarea> as required.

2 - TinyMCE and the <textarea>

TinyMCE does not keep the underlying <textarea> in sync at all times - for most use cases this would add unneeded overhead to the page while someone is creating content.

If you are using a standard HTML form post, when you post the form, TinyMCE will update the <textarea> before the form is posted. Unfortunately, most modern frameworks don't use a standard HTML form post but instead do something using JavaScript/AJAX. You can use the following API call to force TinyMCE to update the <textarea>:

tinymce.triggerSave();

This will force TinyMCE to update the <textarea> when it is called. You can either:

  • Do this in the onsubmit event of the form
  • Do this in the TinyMCE init:

    tinymce.init({
        selector: "textarea",
        setup: function (editor) {
            editor.on('change', function () {
                tinymce.triggerSave();
            });
        }
    });
    



回答2:


One more hack - to inform users that the field is required if they leave it empty. We have to unhidden our textarea. So after Fromin code we add:

init_instance_callback : function(editor) {
        let editorH = editor.editorContainer.offsetHeight;
        $('#formTranslation_trad')
            .css({
                'position':'absolute',
                'height':editorH
            })
            .show();
    },

So: we take height of editor and set this to our textarea (probably we have to do the same on change textarea size?).

We have to set position absolute to avoid design problems and next add width and margins for hide textearea after editor.

When we send empty field there will be tooltip with warning and border on red (or other color depends of system settings).



来源:https://stackoverflow.com/questions/60834085/how-to-make-textarea-filed-mandatory-when-ive-applied-tinymce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!