簡體   English   中英

ASP.NET MVC 3不引人注意的驗證,提交和TinyMCE

[英]ASP.NET MVC 3 unobtrusive validation, submit and TinyMCE

我們為TinyMCE提供了一個內部開發的文件/圖像/文檔管理器插件,該插件仍然被移植到jQuery。 與此同時,我們的一些依賴於這些功能的項目需要使用基於Prototype的TinyMCE和jQuery.noConflict()版本。 這樣可以正常工作,但是在ASP.NET MVC 3中進行不顯眼的驗證時,提交的驗證發生在TinyMCE回調之前,將TinyMCE的內容復制到表單字段被觸發。 是否可以在不顯眼的驗證中掛鈎預驗證事件?

如果您使用提交按鈕發布表單,請嘗試以下操作:

$("input[type='submit']").click(function () {
    tinyMCE.triggerSave();
});

如果你沒有使用提交按鈕,只需勾選在表單提交之前發生的任何事件,並調用tinyMCE.triggerSave()。

在TinyMCE初始化中,另一種可以提供更多控制的方法。 除了一種情況之外,這很有效:退出的最后一個TinyMCE實例不會觸發TinyMCE中的onDeactivate事件(只有當你轉到另一個TinyMCE實例時才會觸發)。 所以這是解決這個問題的一種方法 - 到目前為止它似乎運行良好,但YMMV。

注意:我會將此與已接受的答案結合使用。 當在TinyMCE中編輯內容時,此代碼會觸發驗證。

tinyMCE.init({
    ...
    setup: ValidationTinyMceSetup
});

我們的設置方法:

function ValidationTinyMceSetup(editor) {
    var $textarea = $('#' + editor.editorId);

    // method to use to save editor contents to backend input field (TinyMCE hides real input and syncs it up
    // with values on form submit) -- we need to sync up the hidden input fields and call the valid()
    // method from jQuery unobtrusive validation if it is present
    function save(editor) {
        if (editor.isDirty) {
            editor.save();
            var $input = $('#' + editor.editorId);
            if (typeof $input.valid === 'function')
                $input.valid();
        }
    }

    // Save tinyMCE contents to input field on key/up down (efficiently so IE-old friendly)
    var typingTimerDown, typingTimerUp;
    var triggerDownSaveInterval = 1000;     // time in ms
    var triggerUpSaveInterval = 500;        // time in ms

    editor.onKeyDown.add(function (editor) {
        clearTimeout(typingTimerDown);
        typingTimerDown = setTimeout(function () { save(editor) }, triggerDownSaveInterval);
    });

    editor.onKeyUp.add(function () {
        clearTimeout(typingTimerUp);
        typingTimerUp = setTimeout(function () { save(editor) }, triggerUpSaveInterval);
    });


    // Save tinyMCE contents to input field on deactivate (when focus leaves editor)
    // this is via TAB
    editor.onKeyDown.add(function (editor, event) {
        if (event.keyCode === 9)
            save(editor);
    });

    // this is when focus goes from one editor to another (however the last one never
    // triggers -- need to enter another TinyMCE for event to trigger!)
    editor.onDeactivate.add(function (editor) {
        save(editor);
    });
}

最后,一個完全不相關的獎勵項:您可以通過在JavaScript源中包含此函數來添加字符計數器:

function CharacterCountDisplay(current, max) {
    if (current <= max) {
        return current + ' of ' + max + ' characters max';
    }
    else {
        return '<span style="color: red;">' + current + '</span> of ' + max + ' characters';
    }
}

並在上面的ValidationTinyMceSetup方法中添加:

// check for character count data-val
var character_max = $textarea.attr('data-val-lengthignoretags-max');
if (typeof character_max === 'undefined' || character_max === false) {
    character_max = $textarea.attr('data-val-length-max');
}
if (typeof character_max !== 'undefined' && character_max !== false) {
    var character_count = function (editor) {
        var currentLength = $(editor.getDoc().body).text().length;
        editor.dom.setHTML(tinymce.DOM.get(editor.id + '_path_row'), CharacterCountDisplay(currentLength, character_max));
    };

    // on load show character count
    editor.onInit.add(character_count);

    // while typing update character count
    editor.onKeyUp.add(character_count);
}

要使用簡單的data-val-length-max="250"到你的textarea標簽或者你正在使用TinyMCE的任何東西!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM