Fluent Validation not working for Complex Properties Asp.Net

Go To StackoverFlow.com

2

I am new to Fluent validation and need some help. I Have an Interface that has another interface a property and i have written validations for both interfaces. The challenge is that its not showing validation messages for the interface property.

below is my code for my situation

public interface IAddress
{
    string City { get; set; }
    string Town { get; set; }
}

public interface IAccount
{
    string FullName { get; set; }
    int Age { get; set; }
    IAddress Address { get; set; }
}

    public AccountValidator ()
    {
        RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
        RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
        RuleFor(x => x.Address).SetValidator( new AddressValidator()); 
    }

    public AddressValidator()
    {
        RuleFor(x => x.City).NotEmpty().NotNull().WithMessage("City can not  be empty");
        RuleFor(x => x.Town).NotEmpty().NotNull().WithMessage("Town can not  be empty"); 
    }

My client code i am using to call the validation is :

        var accountValidator = new AccountValidator();

         accountValidator.ValidateAndThrow(_account );

Thanks in advance.

2012-04-04 07:35
by simba


0

Well, I tried a mini console app and exceptions are well catched (age + city) (I juste changed NotEmpty().NotNull() with a Must(...), as you need to put a specific "WithMessage" for each or them).

So did I misunderstand your problem ?

public interface IAddress
    {
        string City { get; set; }
        string Town { get; set; }
    }

    public interface IAccount
    {
        string FullName { get; set; }
        int Age { get; set; }
        IAddress Address { get; set; }
    }

    public class Address : IAddress
    {
        public string City { get; set; }
        public string Town { get; set; }
    }
    public class Account : IAccount
    {
        public string FullName { get; set; }
        public int Age { get; set; }
        public IAddress Address { get; set; }
    }

    public class AccountValidator : AbstractValidator<IAccount>
    {
        public AccountValidator()
        {
            RuleFor(x => x.FullName).NotEmpty().WithMessage("Full Name  can not  be  empty");
            RuleFor(x => x.Age).GreaterThan(18).WithMessage("Age cannot  be  less  than 18 years");
            RuleFor(x => x.Address).SetValidator(new AddressValidator());
        }
    }

    public class AddressValidator : AbstractValidator<IAddress>
    {
        public AddressValidator()
        {
            RuleFor(x => x.City).Must(x => !string.IsNullOrEmpty(x)).WithMessage("City can not  be empty");
            RuleFor(x => x.Town).Must(x => !string.IsNullOrEmpty(x)).WithMessage("Town can not  be empty");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var account = new Account
                               {
                                   Address = new Address
                                                 {
                                                     City = "",
                                                     Town = "Town"
                                                 },
                                   Age = 18,
                                   FullName = "FullName"
                               };
            var accountValidator = new AccountValidator();
            try
            {
                accountValidator.ValidateAndThrow(account);
            }
            catch (ValidationException e)
            {
                Console.WriteLine(e);
            }
            Console.ReadKey();

        }
    }
2012-04-05 09:20
by Raphaël Althaus
Ads