I use FluentValidation in my ASP.NET MVC 4 application. I already knew that some rules automatically generate attributes for the jQuery validation library. And this script library already knew that it should check, for example, in the case data-rule-required, data-rule-rangeetc.
I know that there are some features in FluentValidation, but they are not client-side. For example: .Equal(true). I checked @DarinDimitrov's answer here and implemented it without any problems.
But I do not want to always create a new class that is inherited from FluentValidationPropertyValidator. And we have to add this to a provider like this in global.asax:
provider.Add(typeof(EqualValidator), (metadata, context, description, validator) => new EqualToValueClientRule(metadata, context, description, validator));
In this case, it is EqualValidatoralready implemented in FluentValidation. But what if we created a validator with a keyword When. 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 problems. But this approach is not very good. And in other cases, you need to write so much code to create custom attributes on the client side and add a new function to the adapter. Or just use jQuery / JavaScript? Or something else? Maybe we can add the name of the JavaScript function in FluentValidationPropertyValidator?
What will you advice me?
source
share