Question

This is my Model:

[RegularExpression(@"^08[589][0-9]{8}$", ErrorMessage = "Invalid Number!")]
public string Phone { get; set; }

[ForeignKey]
public long PhoneType { get; set; } // 1-CellPhone , 2-Phone

So I think to change RegularExpression Validation by Change PhoneType if I want say more specific:

if user select CellPhone from DropDownList the validation be

[RegularExpression(@"^08[589][0-9]{8}$", ErrorMessage = "Invalid Number!")] 

and if select Phone the validation be

 [RegularExpression("^[1-9][0-9]{9}$", ErrorMessage = "Invalid Number!")]

What is your suggestion?

Was it helpful?

Solution

You could write a custom validation attribute:

public class PhoneAttribute : ValidationAttribute
{
    private readonly string _phoneTypeProperty;
    public PhoneAttribute(string phoneTyperoperty)
    {
        _phoneTypeProperty = phoneTyperoperty;
    }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var property = validationContext.ObjectType.GetProperty(_phoneTypeProperty);
        if (property == null)
        {
            return new ValidationResult(string.Format("Unknown property: {0}", _phoneTypeProperty));
        }

        var phone = Convert.ToString(value, CultureInfo.CurrentCulture);
        if (string.IsNullOrEmpty(phone))
        {
            return null;
        }

        var phoneType = (long)property.GetValue(validationContext.ObjectInstance, null);
        Regex regex = null;
        if (phoneType == 1)
        {
            regex = new Regex(@"^08[589][0-9]{8}$");
        }
        else if (phoneType == 2)
        {
            regex = new Regex("^[1-9][0-9]{9}$");
        }
        else
        {
            return new ValidationResult(string.Format("Unknown phone type: {0}", phoneType));
        }

        var match = regex.Match(phone);
        if (match.Success && match.Index == 0 && match.Length == phone.Length)
        {
            return null;
        }

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }
}

and then decorate your view model property with this attribute:

public class MyViewModel
{
    [Phone("PhoneType", ErrorMessage = "Invalid Number!")]
    public string Phone { get; set; }

    public long PhoneType { get; set; }
}

Another possibility (and which I would more than strongly recommend) if you want to make your life easier with validation is to use FluentValidation.NET. Just look at how easier it is to define validation rules instead of writing gazzilions of lines of plumbing code and no longer be able to understand which part is plumbing and which part is actual validation. With FluentValidation.NET there's no plumbing. You express your validation requirements in a fluent way:

public class MyViewModelValidator : AbstractValidator<MyViewModel>
{
    public MyViewModelValidator()
    {
        RuleFor(x => x.Phone)
            .Matches(@"^08[589][0-9]{8}$").When(x => x.PhoneType == 1)
            .Matches("^[1-9][0-9]{9}$").When(x => x.PhoneType == 2);
    }
}

Simply compare this validator with the previous one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top