gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:01:08 25 4
gpt4 key购买 nike

我创建了一个派生自 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. 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

最佳答案

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

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

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

这是one example 。这是 another one

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

关于jquery - 使用 ValidationAttribute 的自定义验证不会触发客户端验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9348513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com