Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Pass lambda into rulefor

$
0
0

Hi Guys

Im trying to build a lambda expression and pass this into rulefor. The code compiles, but when executing I get the follwing message..

"'FluentValidation.Internal.RuleBuilder<WTS.WorkSuite.Shared.DTO.User,string>' does not contain a definition for 'Length'"

This is the validation code is this. The aim is that in two validators i want the same validation rule to be applied again username or a-another property.

 

    public class UserValidator : AbstractValidator<DTO.User>
    {
        public UserValidator(DTO.User u)
        {

foreach (PropertyInfo property in
                     this.GetType().BaseType
                         .GetGenericArguments()[0]
                         .GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {


                if (property.Name == "Username")
                {

                    ParameterExpression parameter = Expression.Parameter(typeof(DTO.User), "p");
                    Expression propertyAccess = Expression.Property(parameter, property);
                    // Make it easier to call RuleFor without knowing TProperty
                    dynamic lambda = Expression.Lambda(propertyAccess, parameter);

                    RuleFor(lambda)
                        .Length(4, 9)
                        .WithMessage("Valid between 4 and 9 chars");

                    //RuleFor(x => x.Username)
                    //    .Length(4, 9)
                    //    .WithMessage("Valid between 4 and 9 chars");
                }

            }
        }

 

 

Any help appreciated...


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>