将变量数据传递给 ValidationAttribute
Pass variable data to ValidationAttribute
我需要在更改密码函数中将不同的最小长度值应用于根据用户角色创建的新密码。如果用户没有管理角色,最小长度为 12,如果他们有管理员角色,最小长度为 16。
当前代码没有这样的变量需求逻辑。新密码 属性 的实现类似于模型 class 中的 ChangePasswordData:
///summary>
/// Gets and sets the new Password.
/// </summary>
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength]
[PasswordMinNumber]
public string NewPassword { get; set; }
验证属性设置如下:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public bool IsAdmin { get; set; }
public PasswordMinLength()
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
//Set the default Min Length
this.MinLength = 12;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
我希望能够根据 IsAdmin 的值将 MinLength 的值设置为 12 或 16,但是我不知道如何修饰属性 [PasswordMinLength(IsAdmin=myvarable)].只允许常量。如何将 属性 值注入我可以评估以确定正确的最小长度的 ValidationAttribute?
谢谢!
感谢 Steve Greene 为示例 (http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx) 提供此 link,我得到了验证问题的答案。这是更新后的代码:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public PasswordMinLength(string IsAdminName)
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
IsAdminPropertyName = IsAdminName;
//Set the default Min Length
this.MinLength = 12;
}
public string IsAdminPropertyName{ get; set; }
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name, IsAdminPropertyName);
}
protected bool? GetIsAdmin(ValidationContext validationContext)
{
var retVal = false;
var propertyInfo = validationContext
.ObjectType
.GetProperty(IsAdminPropertyName);
if (propertyInfo != null)
{
var adminValue = propertyInfo.GetValue(
validationContext.ObjectInstance, null);
return adminValue as bool?;
}
return retVal;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (GetIsAdmin(validationContext) != null)
{
if (GetIsAdmin(validationContext) == true)
this.MinLength = 16;
else
this.MinLength = 12;
}
else
this.MinLength = 12;
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
我只是向我的模型 class 添加了一个 IsAdmin 属性 并像这样装饰了 PasswordMinLength 属性:
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength("IsAdmin")]
[PasswordMinNumber]
public string NewPassword { get; set; }
public bool IsAdmin { get; set; }
很有魅力。谢谢史蒂夫!
我需要在更改密码函数中将不同的最小长度值应用于根据用户角色创建的新密码。如果用户没有管理角色,最小长度为 12,如果他们有管理员角色,最小长度为 16。
当前代码没有这样的变量需求逻辑。新密码 属性 的实现类似于模型 class 中的 ChangePasswordData:
///summary>
/// Gets and sets the new Password.
/// </summary>
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength]
[PasswordMinNumber]
public string NewPassword { get; set; }
验证属性设置如下:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public bool IsAdmin { get; set; }
public PasswordMinLength()
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
//Set the default Min Length
this.MinLength = 12;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
我希望能够根据 IsAdmin 的值将 MinLength 的值设置为 12 或 16,但是我不知道如何修饰属性 [PasswordMinLength(IsAdmin=myvarable)].只允许常量。如何将 属性 值注入我可以评估以确定正确的最小长度的 ValidationAttribute?
谢谢!
感谢 Steve Greene 为示例 (http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx) 提供此 link,我得到了验证问题的答案。这是更新后的代码:
/// <summary>
/// Validates Password meets minimum length.
/// </summary>
public class PasswordMinLength : ValidationAttribute
{
public int MinLength { get; set; }
public PasswordMinLength(string IsAdminName)
{
// Set this here so we override the default from the Framework
this.ErrorMessage = ValidationErrorResources.ValidationErrorBadPasswordLength;
IsAdminPropertyName = IsAdminName;
//Set the default Min Length
this.MinLength = 12;
}
public string IsAdminPropertyName{ get; set; }
public override string FormatErrorMessage(string name)
{
return string.Format(ErrorMessageString, name, IsAdminPropertyName);
}
protected bool? GetIsAdmin(ValidationContext validationContext)
{
var retVal = false;
var propertyInfo = validationContext
.ObjectType
.GetProperty(IsAdminPropertyName);
if (propertyInfo != null)
{
var adminValue = propertyInfo.GetValue(
validationContext.ObjectInstance, null);
return adminValue as bool?;
}
return retVal;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
if (GetIsAdmin(validationContext) != null)
{
if (GetIsAdmin(validationContext) == true)
this.MinLength = 16;
else
this.MinLength = 12;
}
else
this.MinLength = 12;
if (value == null || !(value is string) || !UserRules.PasswordMinLengthRule(value.ToString(), this.MinLength))
{
return new ValidationResult(this.ErrorMessageString, new string[] { validationContext.MemberName });
}
return ValidationResult.Success;
}
}
我只是向我的模型 class 添加了一个 IsAdmin 属性 并像这样装饰了 PasswordMinLength 属性:
[Display(Order = 3, Name = "NewPasswordLabel", ResourceType = typeof(UserResources))]
[Required]
[PasswordSpecialChar]
[PasswordMinLower]
[PasswordMinUpper]
[PasswordMaxLength]
[PasswordMinLength("IsAdmin")]
[PasswordMinNumber]
public string NewPassword { get; set; }
public bool IsAdmin { get; set; }
很有魅力。谢谢史蒂夫!