2

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?

Saeid
  • 13,224
  • 32
  • 107
  • 173

1 Answers1

6

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • is FluentValidation.NET introduce easy way as well to register Client side unobtrusive validation ? – Wahid Bitar May 07 '12 at 12:11
  • @WahidBitar, yes, FluentValidation.NET supports the same client validation rules as data annotations. – Darin Dimitrov May 07 '12 at 13:00
  • @DarinDimitrov Thanks for answer, but there is some issues to work with FluentValidation.Net, first: as Documentation says: FluentValidation will also work with ASP.NET MVC's client-side validation, but not all rules are supported. For example, any rules defined using a condition (with When/Unless), custom validators, or calls to Must will not run on the client side. and Second: I can't control ErrorMessage in client side. Am I right? – Saeid May 07 '12 at 13:07
  • 1
    @Saeid, yes, you are right. But don't think that with the custom attribute that I have shown in my answer you will get any client side validation at all. You will have to write a custom client side adapter to make this work. Because this validator depends on the value of another property. FluentValidation.NET supports the same validation rules out of the box as data annotations. For everything else you will have to write javascript adapters the same way you will have to with the data annotations. Here's an example of what you need in both cases: http://stackoverflow.com/a/7854915/29407 – Darin Dimitrov May 07 '12 at 13:09