I am using FluentValidation in my Asp.Net MVC 4 application. I have already known that some rules are automatically generating attributes for jQuery validation library. And this script library has already known what it must check for example in case of data-rule-required
, data-rule-range
and so on.
I know that there are some functions in FluentValidation, but these are not include for client-side. For example: .Equal(true)
.
I have checked @DarinDimitrov answer here and implemented this without any problem.
But, I don't want always to create new class which is inherited from FluentValidationPropertyValidator
. And we must add this to provider like that in global.asax:
provider.Add(typeof(EqualValidator), (metadata, context, description, validator) => new EqualToValueClientRule(metadata, context, description, validator));
In this case EqualValidator
already implemented in FluentValidation. But, what if we have created a validator with When
keyword. For example, I have:
this.RuleFor(phone => phone.Digits)
.Length(7)
.When(phone => phone.PrefixId == 2)
.WithMessage("Numbers in 2nd city must contain 7 characters");
this.RuleFor(phone => phone.Digits)
.Length(7)
.When(phone => phone.PrefixId > 64)
.WithMessage("Mobile number must contain 7 characters");
this.RuleFor(phone => phone.Digits)
.Length(5)
.When(phone => phone.PrefixId != 2)
.WithMessage("Numbers in other cities must contain 5 characters")
Of course, I can check this with jQuery/JavaScript without any problem. But, this approach is not good. And in other cases you have to write so much code for generating custom attributes in client side and add new function to adapter. Or, just use jQuery/JavaScript? Or any other thing? May be we can add JavaScript function name to FluentValidationPropertyValidator
?
What do you recommend me?