简体   繁体   English

将 asp mvc3 不显眼的验证与 jquery 验证插件混合使用

[英]Mixing asp mvc3 unobtrusive validation with jquery validation plugin

For asp mvc3 client side validation, is it possible to mix the default, out of the box unobtrusive style with the jquery validation plugin?对于 asp mvc3 客户端验证,是否可以将默认的、开箱即用的不显眼样式与 jquery 验证插件混合使用?

This is the form in the view:这是视图中的表单:

@using (Html.BeginForm("", "", FormMethod.Post, new { id = "newRatingForm" }))
{   
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Rating</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Rate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Rate)
            @Html.ValidationMessageFor(model => model.Rate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

Unobtusive works great by default using the usual validation attributes on the properties of the model.默认情况下,使用 model 属性上的常用验证属性,Unobtusive 效果很好。 The goal is to replace the 'model.Rate' bits in the form with [fyneworks][1] jquery star rating plugin and use the jquery validation plugin just for that part of the form:目标是用 [fyneworks][1] jquery 星级插件替换表单中的“model.Rate”位,并仅针对表单的该部分使用 jquery 验证插件:

<span>Your Rating:</span>
<div id="ratingStars">
    <input class="star {split:2}" type="radio" name="Rating" value="0.5"/>
    <input class="star {split:2}" type="radio" name="Rating" value="1.0"/>
    <input class="star {split:2}" type="radio" name="Rating" value="1.5"/>
    <input class="star {split:2}" type="radio" name="Rating" value="2.0"/>
    <input class="star {split:2}" type="radio" name="Rating" value="2.5"/>
    <input class="star {split:2}" type="radio" name="Rating" value="3.0"/>
    <input class="star {split:2}" type="radio" name="Rating" value="3.5"/>
    <input class="star {split:2}" type="radio" name="Rating" value="4.0"/>
    <input class="star {split:2}" type="radio" name="Rating" value="4.5"/>
    <input class="star {split:2}" type="radio" name="Rating" value="5.0"/>
</div>

I've hijacked the submit action for the form and converted form values to Json to do an ajax request with:我劫持了表单的提交操作并将表单值转换为 Json 以执行 ajax 请求:

$("#newRatingForm").submit(function (event) {
    event.preventDefault();

    if ($(this).valid()) {        
        newRatingSubmit();
    }    
}); 

function newRatingSubmit() {
    //jquery ajax request using form values converted to Json
}

Radio buttons seem to break the form and the hijack code no longer works ie when I click submit, no request is made to server (watching on firebug console) and the form just disappears.单选按钮似乎破坏了表单,劫持代码不再起作用,即当我单击提交时,没有向服务器发出请求(在 firebug 控制台上观看),表单就消失了。 Again, it all works fine with just plain mvc3 form.同样,只需简单的 mvc3 形式就可以正常工作。

I tried adding a jquery validation rule for the rating radio buttons but didn't seem to make any difference:我尝试为评级单选按钮添加 jquery 验证规则,但似乎没有任何区别:

$("#newRatingForm").validate({
    meta: "validate", 
    errorLabelContainer: "#ErrorDiv",
    wrapper: "div",
    rules:
    {
        Rating: {
            required: true
        }
    },

    messages:
    {
        Rating: {
            required: 'A Rating required.'
        }
    },

    onfocusout: false,
    onkeyup: false,

    //handle validation ok, POST submit
    submitHandler: function (label) {
        newRatingSubmit();
    }
 });

As an alternative I can validate a standard html form using the star rating plugin using the jquery validation plugin and submit that with the above code but that doesnt seem very 'asp.net mvc'.作为替代方案,我可以使用 jquery 验证插件使用星级插件验证标准 html 表单,并使用上述代码提交,但这似乎不是'asp.net mvc'。 For example I then need 2 sets of validation messages - 1 for the server using validation attributes and another for the client view.例如,我需要两组验证消息——一组用于使用验证属性的服务器,另一组用于客户端视图。

jQuery.validator.addMethod("methodeName", function (value, element, params) {
        if (somthing) {
            return false;
        }
        return true;
    });

    jQuery.validator.unobtrusive.adapters.addBool("methodeName");

and in html add 2 attributes并在 html 添加 2 个属性

dal-val = true and dal-val-methodeName="errorMessage" dal-val = true 和dal-val-methodeName="errorMessage"

As suggested by @Vadim, I decorated the star rating controls of my form with data-val="true" data-val-required="Please enter your rating" as in;正如@Vadim 所建议的那样,我用 data-val="true" data-val-required="Please enter your rating" 装饰了我的表单的星级控件,如下所示;

<div id="ratingStars">
        <input class="star {split:2}" type="radio" name="Rating" value="0.5" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="1.0" data-val="true"data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="1.5" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="2.0" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="2.5" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="3.0" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="3.5" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="4.0" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="4.5" data-val="true" data-val-required="Please enter your rating"/>
        <input class="star {split:2}" type="radio" name="Rating" value="5.0" data-val="true" data-val-required="Please enter your rating"/>
    </div>
    <span data-valmsg-replace="true" data-valmsg-for="Rating" class="field-validation-valid" style="display:block; width:80px"></span>

I dont need a custom validator I think as the jquery default 'required' validator will do fine.我不需要自定义验证器,我认为 jquery 默认的“必需”验证器就可以了。 This kinda works.这有点工作。 However, the 1st time I click submit I get the exact result Im hoping for;但是,第一次单击提交时,我得到了我希望的确切结果;

第一次提交验证正常

If I then select a rating and click submit again (without, say, inputting an email) the form just 'disappears' from view and my controller is not hit.如果然后我对 select 进行评级并再次单击提交(例如,不输入电子邮件),表单就会从视图中“消失”,并且我的 controller 没有被击中。

If I replace the radio buttons with a plain text box instead to accept the rating;如果我用纯文本框替换单选按钮来接受评级;

<input type="text" name="Rating" id="Rating" data-val-required="Please enter your rating" data-val="true" class="text-box single-line"/>

it works great and my controller is hit when I click submit.它工作得很好,当我点击提交时,我的 controller 被击中。 Text box no good for the rating though unfortunately.不幸的是,文本框对评级没有好处。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM