如何添加一个新的验证规则mvcClientValidationMetadata? [英] How to add a new validation rule to mvcClientValidationMetadata?

查看:103
本文介绍了如何添加一个新的验证规则mvcClientValidationMetadata?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要相等(密码和密码确认)两个字段的形式。我创建了一个类属性来检查和服务器端它的伟大工程。在客户端,它什么都不做。我需要的消息出现在的ValidationSummary(密码重复需要是相同的密码)。

I have a form with two fields that need to be equal (password and password confirmation). I've created a class attribute to check that and on server side it works great. On the client side it does nothing. I need the message to appear in ValidationSummary ("Password repeated" needs to be the same as "Password").

我意识到,要检查这些字段的最简单的方法将手动添加规则window.mvcClientValidationMetadata。我试图这样做,但毫无效果。

I realized that the easiest way to check these fields would be adding the rule manually to window.mvcClientValidationMetadata. I was trying to do that but nothing worked.

我的code:

<% using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post}))
   { %>
<%= Html.ValidationSummary() %>
    <div>
        <%= Html.ValidationMessageFor(m => m.Email)%>
        <%= Html.LabelFor(m => m.Email)%>
    </div>
    <div>
        <%= Html.TextBoxFor(m => m.Email)%>
        <% Html.ValidateFor(m => m.Email);%>
    </div>
    <div>
        <%= Html.ValidationMessageFor(m => m.PasswordModel.Password)%>
        <%= Html.LabelFor(m => m.PasswordModel.Password)%>
    </div>
    <div>
        <%= Html.PasswordFor(m => m.PasswordModel.Password)%>
    </div>
    <div>
        <%= Html.ValidationMessageFor(m => m.PasswordModel.PasswordRepeated)%>
        <%= Html.LabelFor(m => m.PasswordModel.PasswordRepeated)%>
    </div>
    <div>
        <%= Html.PasswordFor(m => m.PasswordModel.PasswordRepeated)%>
    </div>
    <div>
        <%= Html.ValidationMessageFor(m => m.PasswordModel.PasswordRepeated)%>
        <%= Html.LabelFor(m => m.PasswordModel.PasswordRepeated, true)%>
    </div>
<% } %>

Html.EnableClientValidation 产生这种形式之前执行的方法。

Html.EnableClientValidation method is executed before this form is generated.

下面你会找到解决我的问题。

Below you'll find the solution to my problem.

推荐答案

您可以做的最糟糕的事情是exacly执行相同的code - 你会覆盖现有规则

The worst thing you may do is to execute exacly the same code - you'll overwrite the existing rules.

要添加你需要把这个code右后&LT验证规则;%}%GT; 结束使用(... 您BeginForm的:

To add validation rules you need to put this code right after <% } %> closing using(... of your BeginForm:

<% } %>

<script type="text/javascript">

    window.mvcClientValidationMetadata[0]["Fields"].push( //check if the '0' is number corresponding to your form validation metadata
      {
          "FieldName": "PasswordModel.PasswordRepeated", // name attribute of the element
          "ReplaceValidationMessageContents": false,
          "ValidationMessageId": "PasswordModel_PasswordRepeated_validationMessage", //id of the ValidationMessageFor (if needed)
          "ValidationRules":
          [
            {
                "ErrorMessage": 'Password repeated needs to be the same as Password',
                "ValidationParameters": "#PasswordModel_Password", //'params' parameter in your validation function, can be an object
                "ValidationType": "propertiesMustMatch" //name of the validation function placed in $.validator.methods
            }
          ]
      }
    );

</script>

propertiesMustMatch功能检查,如果给定的字段是相等的(jQuery的equalTo didin't在我们的系统中正常工作)。

propertiesMustMatch function checks if given fields are equal (jQuery equalTo didin't work correctly in our system).

有没有遗漏的类型错误:无法调用未定义的方法推的例外,因为mvcClientValidationMetadata在&LT生成;脚本&GT; 元素放到哪里&LT;%}%方式&gt;

There is no "Uncaught TypeError: Cannot call method 'push' of undefined" exception, because mvcClientValidationMetadata is generated in <script> element put where <% } %> is.

这篇关于如何添加一个新的验证规则mvcClientValidationMetadata?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆