基本上,对于后端,您需要创建一个自定义验证,该验证可用作要验证的字段的属性。
这是检查“数字是否大于另一个字段”的验证示例。
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
public class NumberGreaterThanAttribute : ValidationAttribute, IClientValidatable
{
string otherPropertyName;
public NumberGreaterThanAttribute(string otherPropertyName, string errorMessage)
: base(errorMessage)
{
this.otherPropertyName = otherPropertyName;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
ValidationResult validationResult = ValidationResult.Success;
if (value == null)
return validationResult;
try
{
// Using reflection we can get a reference to the other date property, in this example the project start date
var otherPropertyInfo = validationContext.ObjectType.GetProperty(this.otherPropertyName);
object referenceProperty = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null);
if (referenceProperty == null)
return validationResult;
double referenceProperty_value = System.Convert.ToDouble(referenceProperty, null);
double currentField_value = System.Convert.ToDouble(value, null);
if (currentField_value <= referenceProperty_value)
{
validationResult = new ValidationResult(ErrorMessageString);
}
}
catch (Exception ex)
{
throw ex;
}
return validationResult;
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
//string errorMessage = this.FormatErrorMessage(metadata.DisplayName);
string errorMessage = ErrorMessageString;
// The value we set here are needed by the jQuery adapter
ModelClientValidationRule numberGreaterThanRule = new ModelClientValidationRule();
numberGreaterThanRule.ErrorMessage = errorMessage;
numberGreaterThanRule.ValidationType = "numbergreaterthan"; // This is the name the jQuery adapter will use
//"otherpropertyname" is the name of the jQuery parameter for the adapter, must be LOWERCASE!
numberGreaterThanRule.ValidationParameters.Add("otherpropertyname", otherPropertyName);
yield return numberGreaterThanRule;
}
}
记住:对于自定义/新创建的验证,不会自动启用前端验证。如果你想为前端创建相同的验证规则,你需要创建一个新的前端自定义验证。我通常使用 validate.js 库作为前端。尽管如此,如果您不介意从服务器到客户端来回推送数据,特别是在数据很小的情况下,前端验证并不是严格要求的(但我建议在任何情况下都使用它)。
ViewModel/Model 会在后端收到请求(一般是 POST)时被 modelstate 接收和解析。模型/视图模型应该在需要验证的字段上进行修饰:
public Int64? AtomicQtyMultiplePurchaseMin { get; set; }
[NumberGreaterThanAttribute("AtomicQtyMultiplePurchaseMin", "Must be greater than min num. qty. of purchase")]
public Int64? AtomicQtyMultiplePurchaseMax { get; set; }
我一般将自定义验证类放在以下文件夹中(但你可以将它放在项目中你想要的位置):