0

I have a customer class like this:

[Validator(typeof(CustomerValidator))]
public partial class Customer {  
    public string FirstName { get; set; }  
    public string LastName { get; set; }
}  

And my Validation class is:

public class CustomerValidator : AbstractValidator<Customer>
    {
        public CustomerValidator()
        {
            RuleFor(x => x.FirstName)
                .NotEmpty()                  
                .WithMessage("FirstName is required."));
            RuleFor(x => x.LastName)
                .NotEmpty()                  
                .WithMessage("LastName is required."));
        }
    }

Everything works fine, the code did validate fields.
I planned to extend the Customer class and add Email field. I cannot edit my old code file. I create new partial Customer class and new validation for Email field.
I can create partial customer class like this:

public partial class Customer {  
        public string Email { get; set; }  
    }

But I don't know how to validate this field via another class. The code below is what i expected but it I dont know how to bind it in the Customer Email field:

  public class CustomerEmailValidator : AbstractValidator<Customer>
            {
                public CustomerEmailValidator()
                {
                    RuleFor(x => x.Email)
                        .EmailAddress()                  
                        .WithMessage("Email address is not valid."));
                }
            }

All helps are appreciated.
Thanks in advance.

akari
  • 149
  • 9

1 Answers1

0

Put [Validator(typeof(CustomerValidator))] annotation on the partial class as well.

Greg
  • 2,654
  • 3
  • 36
  • 59