验证取决于其他字段的值(MVC)

时间:2016-07-18 23:24:23

标签: c# regex validation

我有3个输入框可供某人输入电话号码。一个用于区号(3位),一个用于前缀(3位),一个用于后缀(4位)。我想在保存之前验证3个字段的总和等于10。如何使用数据注释完成?

模型:

 public string PhoneNumber
    {
        get
        {
            return _phoneNumber;
        }
        set
        {
            _phoneNumber = value;
        }
    }      
    private string _phoneNumber;
    public string Area
    {
        get
        {
            try
            {
                return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim();
            }
            catch
            {
                return "";
            }
        }

    }

    public string Prefix
    {
        get
        {
            try
            {
                return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[1].Trim();
            }
            catch
            {
                return "";
            }
        }

    }

    public string Suffix
    {
        get
        {
            try
            {
                return _phoneNumber.Split(new char[] { '(', ')', '-' }, StringSplitOptions.RemoveEmptyEntries)[2].Trim();
            }
            catch
            {
                return "";
            }
        }

    }

2 个答案:

答案 0 :(得分:1)

你可以这样做:

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string area = (string)validationContext.ObjectType.GetProperty("Area").GetValue(validationContext.ObjectInstance, null);
        string prefix = (string)validationContext.ObjectType.GetProperty("Prefix").GetValue(validationContext.ObjectInstance, null);
        string suffix = (string)validationContext.ObjectType.GetProperty("Suffix").GetValue(validationContext.ObjectInstance, null);

        if ((area.Length + prefix.Length + suffix.Length) == 10)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult("I will not use DA for this, but there we go...");
        }
    }

首先连接值,然后使用属性值

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string number = (string)value;
        if (number.Length == 10)
        {
            return ValidationResult.Success;
        }
        else
        {
            return new ValidationResult("I will not use DA for this, but there we go...");
        }
    }

答案 1 :(得分:1)

这里有两种可能的方法。

IValidatableObject

正如用户stuartd在评论中所提到的,您可以在模型中实施IValidatableObject来进行验证。 在您的情况下,您的代码看起来像这样:

public class MyModel : IValidatableObject
{
    // Your properties go here

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        // You may want to check your properties for null before doing this
        var sumOfFields = PhoneNumber.Length + Area.Length + Prefix.Length;

        if(sumOfFields != 10)
            return new ValidationResult("Incorrect phone number!");
    }
}

Custom ValidationAttribute

由于您声明要使用数据注释,因此可以实现自定义ValidationAttribute。 它会沿着这些方向发展。

public class TotalAttributesLengthEqualToAttribute : ValidationAttribute
{
    private string[] _properties;
    private int _expectedLength;
    public TotalAttributesLengthEqualToAttribute(int expectedLength, params string[] properties)
    {
        ErrorMessage = "Wrong total length";
        _expectedLength = expectedLength;
        _properties = properties;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        if (_properties == null || _properties.Length < 1)
        {
            return new ValidationResult("Wrong properties");
        }

        int totalLength = 0;

        foreach (var property in _properties)
        {
            var propInfo = validationContext.ObjectType.GetProperty(property);

            if (propInfo == null)
                return new ValidationResult($"Could not find {property}");

            var propValue = propInfo.GetValue(validationContext.ObjectInstance, null) as string;

            if (propValue == null)
                return new ValidationResult($"Wrong property type for {property}");

            totalLength += propValue.Length;
        }

        if (totalLength != _expectedLength)
            return new ValidationResult(ErrorMessage);

        return ValidationResult.Success;
    }
}

比您选择一个属性并实现它:

[TotalAttributesLengthEqualTo(10, nameof(PhoneNumber), nameof(Area), nameof(Prefix), ErrorMessage = "The phone number should contain 10 digits")]
public string PhoneNumber
{
   get...

请注意,如果您的编译器不支持C#6.0,则必须将以$开头的字符串更改为string.Format,并且您必须将nameof()中的属性名称替换为其他硬编码名称。