使用 ValidationAttribute 的自定义验证不会触发客户端验证

发布于 2025-01-07 04:36:31 字数 4946 浏览 2 评论 0原文

我创建了一个派生自 ValidationAttribute 的自定义验证器。我的理解是,它将生成足够的元数据供客户端脚本自动验证(使用 jquery.validate)。自定义验证器在服务器端工作正常。但它不会在客户端触发错误消息。 (其他默认验证器如“StringLength”在客户端也工作正常。)我们如何纠正它?

public class Person
{
    [Required(ErrorMessage = "First name required")]
    public string FirstName { get; set; }

    [CustomStartLetterMatch("FirstName")]
    [StringLength(5,ErrorMessage = "Must be under 5 characters")]
    public string LastName { get; set; }

    [Range(18,50,ErrorMessage="Must be between 18 and 50")]
    public int Age { get; set; }


}


public sealed class CustomStartLetterMatch : ValidationAttribute
{

    private const string _defaultErrorMessage = " First letter of '{0}' must be same as first letetr of '{1}'";
    private string _basePropertyName;

    public CustomStartLetterMatch(string basePropertyName)
        : base(_defaultErrorMessage)
    {
        _basePropertyName = basePropertyName;
    }


    //Override FormatErrorMessage Method
    public override string FormatErrorMessage(string name)
    {
        return string.Format(_defaultErrorMessage, name, _basePropertyName);
    }


    //Override IsValid
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //Get PropertyInfo Object
        var basePropertyInfo = validationContext.ObjectType.GetProperty(_basePropertyName);
        var baseValue = (string)basePropertyInfo.GetValue(validationContext.ObjectInstance, null);
        var currentValue = (string)value;


        string firstLetterBaseValue = baseValue.Substring(0, 1);
        string firstLetterCurrentValue = currentValue.Substring(0, 1);

        //Comparision
        if (!string.Equals(firstLetterBaseValue, firstLetterCurrentValue))
        {
            var message = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        //Default return - This means there were no validation error
        return null;
    }

}

查看阅读

@model MyValidationTEST.Person

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript">  </script>

@*UnObtrusive*@
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>




@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Person</legend>

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

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

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



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

<div>
@Html.ActionLink("Back to List", "Index")
</div>

内容:

  1. MVC3 中的 IValidatableObject - 客户端验证

  2. 在 ASP 中.NET MVC3 如何在非常相似但略有不同的视图模型中保持 DRY?

  3. http://odetocode.com/Blogs/scott/archive/2011/02/22/custom-data-annotation-validator-part-ii-client-code.aspx

  4. http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

  5. 带参数的 ASP.NET MVC 3 客户端验证

  6. < p>如何拥有自定义ValidationAttribute 在客户端呈现为“da​​ta-val-xx”属性?

  7. ASP.NET-MVC3 中“自我验证模型”中的客户端验证

I have created a custom validator deriving from ValidationAttribute. My undertsandng is that it will generate enough meta data for the client side script to automatically validate (using jquery.validate). The custom validator is working fine in server side. But it does not fire error message in the client side. (Other default validators like “StringLength“ are working fine in the client side too.) How do we correct it?

public class Person
{
    [Required(ErrorMessage = "First name required")]
    public string FirstName { get; set; }

    [CustomStartLetterMatch("FirstName")]
    [StringLength(5,ErrorMessage = "Must be under 5 characters")]
    public string LastName { get; set; }

    [Range(18,50,ErrorMessage="Must be between 18 and 50")]
    public int Age { get; set; }


}


public sealed class CustomStartLetterMatch : ValidationAttribute
{

    private const string _defaultErrorMessage = " First letter of '{0}' must be same as first letetr of '{1}'";
    private string _basePropertyName;

    public CustomStartLetterMatch(string basePropertyName)
        : base(_defaultErrorMessage)
    {
        _basePropertyName = basePropertyName;
    }


    //Override FormatErrorMessage Method
    public override string FormatErrorMessage(string name)
    {
        return string.Format(_defaultErrorMessage, name, _basePropertyName);
    }


    //Override IsValid
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        //Get PropertyInfo Object
        var basePropertyInfo = validationContext.ObjectType.GetProperty(_basePropertyName);
        var baseValue = (string)basePropertyInfo.GetValue(validationContext.ObjectInstance, null);
        var currentValue = (string)value;


        string firstLetterBaseValue = baseValue.Substring(0, 1);
        string firstLetterCurrentValue = currentValue.Substring(0, 1);

        //Comparision
        if (!string.Equals(firstLetterBaseValue, firstLetterCurrentValue))
        {
            var message = FormatErrorMessage(validationContext.DisplayName);
            return new ValidationResult(message);
        }

        //Default return - This means there were no validation error
        return null;
    }

}

VIEW

@model MyValidationTEST.Person

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript">  </script>

@*UnObtrusive*@
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>




@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Person</legend>

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

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

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



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

<div>
@Html.ActionLink("Back to List", "Index")
</div>

READING:

  1. IValidatableObject in MVC3 - client side validation

  2. In ASP.NET MVC3 how do you stay DRY with very similar but slightly different viewmodels?

  3. http://odetocode.com/Blogs/scott/archive/2011/02/22/custom-data-annotation-validator-part-ii-client-code.aspx

  4. http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

  5. ASP.NET MVC 3 client-side validation with parameters

  6. How can I have a custom ValidationAttribute rendered as a 'data-val-xx' attribute on the client-side?

  7. Clientside Validation in "Self Validate Model" in ASP.NET-MVC3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-01-14 04:36:31

我的理解是,它将生成足够的元数据
自动验证的客户端脚本(使用 jquery.validate)。

你的理解是错误的。您不可能期望有足够的元数据来生成客户端验证。在这个 IsValid 方法中,您绝对可以做任何事情。您甚至可以调用非托管 C++ 库来执行验证。您不可能期望 ASP.NET MVC 3 会在客户端上反映这一点。

您需要实现 IClientValidatable 并添加自定义适配器(如果您想为此类自定义验证逻辑启用客户端验证)。在此适配器中,您必须重新实现与服务器上相同的逻辑,但这次使用 javascript。

这是一个示例。这是另一个

正如您所看到的,客户端验证可以很好地处理一些简单的规则,例如必需的和其他内容,一旦您开始进行一些真正的验证,您就必须自己实现它。

My undertsandng is that it will generate enough meta data for the
client side script to automatically validate (using jquery.validate).

Your understanding is wrong. You cannot possibly expect that there's enough metadata in order to generate client validation. In this IsValid method you could do absolutely anything. You could even call an unmanaged C++ library to perform validation. You cannot possibly expect that ASP.NET MVC 3 will reflect this on the client.

You need to implement IClientValidatable and add a custom adapter if you want to enable client validation for such custom validation logic. In this adapter you have to reimplement the same logic you did on the server but using javascript this time.

Here's one example. And here's another one.

As you can see client side validation works fine with some simple rules such as Required and stuff, bit once you start doing some real validation you will have to implement it yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文