gpt4 book ai didi

asp.net-mvc-3 - 实现RequiredIf 和RequiredIfNot 属性验证器,这取决于两个值

转载 作者:行者123 更新时间:2023-12-04 05:27:14 27 4
gpt4 key购买 nike

我需要实现一个 RequiredIF 验证器,它取决于两个值,一个复选框和一个从下拉列表中选择的值。
如果可能的话,我需要类似的东西

  [RequiredIf("Property1", true,"Property2,"value", ErrorMessageResourceName = "ReqField", ErrorMessageResourceType = typeof(RegisterUser))]

最佳答案

这是创建您自己的 RequiredIf 和 RequiredIfNot 自定义验证器的代码。
如果您想检查 2 个值,只需添加额外的代码。

必填如果

public class RequiredIfAttribute : ValidationAttribute, IClientValidatable
{
private readonly RequiredAttribute _innerAttribute = new RequiredAttribute();

internal string _dependentProperty;
internal object _targetValue;

public RequiredIfAttribute(string dependentProperty, object targetValue)
{
_dependentProperty = dependentProperty;
_targetValue = targetValue;
}

/// <summary>
/// Returns if the given validation result is valid. It checks if the RequiredIfAttribute needs to be validated
/// </summary>
/// <param name="value">Value of the control</param>
/// <param name="validationContext">Validation context</param>
/// <returns></returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var field = validationContext.ObjectType.GetProperty(_dependentProperty);
if (field != null)
{
var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
if ((dependentValue == null && _targetValue == null) || (dependentValue.ToString() == _targetValue.ToString()))
{
if (!_innerAttribute.IsValid(value))
{
return new ValidationResult(ErrorMessage);
}
}
return ValidationResult.Success;
}
else
{
throw new ValidationException("RequiredIf Dependant Property " + _dependentProperty + " does not exist");
}
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessageString,
ValidationType = "requiredif",
};
rule.ValidationParameters["dependentproperty"] = (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(_dependentProperty);
rule.ValidationParameters["desiredvalue"] = _targetValue is bool ? _targetValue.ToString().ToLower() : _targetValue;

yield return rule;
}
}

RequireIfNot:
public class RequiredIfNotAttribute : ValidationAttribute, IClientValidatable
{
private readonly RequiredAttribute _innerAttribute = new RequiredAttribute();

internal string _dependentProperty;
internal object _targetValue;

public RequiredIfNotAttribute(string dependentProperty, object targetValue)
{
_dependentProperty = dependentProperty;
_targetValue = targetValue;
}

/// <summary>
/// Returns if the given validation result is valid. It checks if the RequiredIfAttribute needs to be validated
/// </summary>
/// <param name="value">Value of the control</param>
/// <param name="validationContext">Validation context</param>
/// <returns></returns>
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var field = validationContext.ObjectType.GetProperty(_dependentProperty);
if (field != null)
{
var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
if ((dependentValue == null && _targetValue == null) || (dependentValue.ToString() != _targetValue.ToString()))
{
if (!_innerAttribute.IsValid(value))
{
return new ValidationResult(ErrorMessage);
}
}
return ValidationResult.Success;
}
else
{
throw new ValidationException("RequiredIfNot Dependant Property " + _dependentProperty + " does not exist");
}
}

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = ErrorMessageString,
ValidationType = "requiredifnot",
};
rule.ValidationParameters["dependentproperty"] = (context as ViewContext).ViewData.TemplateInfo.GetFullHtmlFieldId(_dependentProperty);
rule.ValidationParameters["desiredvalue"] = _targetValue is bool ? _targetValue.ToString().ToLower() : _targetValue;

yield return rule;
}
}

用法:
在您的模型中使用以下 DataAnnotation。
[RequiredIf("IsRequired", true, ErrorMessage = "First Name is required.")]

关于asp.net-mvc-3 - 实现RequiredIf 和RequiredIfNot 属性验证器,这取决于两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13059520/

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