无法在MVC4中验证TinyMCE 4控件

时间:2014-06-10 22:04:34

标签: asp.net-mvc validation asp.net-mvc-4 unobtrusive-validation tinymce-4

我使用MVC4与 knockoutjs (带映射插件)和 tinymce 的绑定插件(定义&#34; wysiwyg < / em>&#34;绑定将textarea与 TinyMCE 编辑器相关联。一切正常,但我没有成功尝试在 TinyMCE 控件中进行不显眼的验证。正如您将看到的,我已经应用了这里和谷歌中提出的几个建议的解决方案,但都没有。

TinyMCE (以及 TinyMCE jquery )版本为4.0.26

代码的相关部分如下:

.CS (仅限与TinyMCE控件关联的属性)

    [AllowHtml]
    [LocRequired]
    [LocDisplayName(Consts.LBL_TXT_EN)]
    public string Text_en { get; set; }

&#34; Loc &#34;前缀属性继承自 RequiredAttribute DisplayNameAttribute 数据注释类,并且还可以从数据库中检索本地化文本。它们工作正常,因此假设它们是常规必需 DisplayName 属性。还有其他类似的属性: Text_es Text_de 等,它们应具有类似的属性集,但是现在我只设置 LocRequired < / em>在 Text_en 中,直到问题解决。

.CSHTML:

<td>
    <div class="editor-label required">
        @Html.LabelFor(model => model.Text_en)
    </div>
    <div class="editor-field">
        @Html.TextAreaFor(model => model.Text_en,new{data_bind="wysiwyg:Text_en"})
        @Html.ValidationMessageFor(model => model.Text_en)
    </div>
</td>

我有几个,对于上面提到的其他属性,每个属性当然都有相应的设置。

javascript: (请参阅代码中的评论)。我已经应用了&#34;忽略&#34;验证器的选项,tinymce.triggerSave()和form.validate()定义,没有任何作用

$(function()
{
...
    var mapping = 
    {
        create: function(options)
        {
            var vm = ko.mapping.fromJS(options.data);
            ...
            ...
            vm.acceptDataEdit = function() //this is associated with the click event "Save" button in the form
            {
                tinymce.triggerSave(); //Almost every search on google says it solves the problem... Well, here it doesn't
                var ok = frm.valid(); //Validation works for other fields but not for the tinymce
                if (ok)
                {
                    ...//posts the data
                }
                return false;
            }

            ko.editable(vm);// ko.editable(this);
            return vm;
        }
    }

        ko.bindingHandlers['wysiwyg'].defaults =
    {

    theme: "modern",
    plugins: [
         "advlist autolink link image lists charmap print preview hr anchor pagebreak spellchecker",
         "searchreplace  visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
         "table directionality emoticons template paste textcolor"
    ],
    forced_root_block : false,
    force_br_newlines : true,
    force_p_newlines : false,
    content_css: "css/content.css",
    toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | l      ink image | print preview media fullpage | forecolor backcolor emoticons", 
    style_formats: [
         {title: 'Bold text', inline: 'b'},
         {title: 'Red text', inline: 'span', styles: {color: '#ff0000'}},
         {title: 'Red header', block: 'h1', styles: {color: '#ff0000'}},
         {title: 'Example 1', inline: 'span', classes: 'example1'},
         {title: 'Example 2', inline: 'span', classes: 'example2'},
         {title: 'Table styles'},
         {title: 'Table row 1', selector: 'tr', classes: 'tablerow1'}
    ]
    };

    var viewModel = ko.mapping.fromJS(vData, mapping);
    ko.applyBindings(viewModel);

    $.validator.setDefaults({ ignore: '' }); // again, this is supposed to make the validator process the hidden text areas, but nothing happens

});

我还尝试在 setDefaults 行之后添加,同样没有结果:

frm.validate({
    rules: {
        Text_en:
        {
            required: true
        }
    }});

还有其他建议吗?我在这里有什么问题吗?

1 个答案:

答案 0 :(得分:1)

终于奏效了。感谢@marathonman,我重新检查了link he provided的帖子(之前,被接受的帖子是我已经完成的内容),但这次我还看了Brian Surowiec的帖子(I&# 39;当我有足够的积分可以投票时,我会投票支持这两个帖子,并遵循他的建议,让textareas在屏幕外显示是什么使它工作。所以我添加了他所做的:

var offCss = { position: 'absolute', height: 0, width: 0, top: -100 };
$('#Text_en').css(offCss);
$('#Text_es').css(offCss);
...//and the same for the others

//after that called show() for them
$('#Text_en').show();
$('#Text_es').show();
...//and so on

就是这样。顺便说一句:在我发布的其他代码中,我之前尝试过的frm.validate代码根本不需要,其他一切都保留了。