I have the following rules
the 1st does work using unobtrusive, client side validation, the second does not
any ideas why?
RuleFor(x => x.StartDate)
.LessThanOrEqualTo(x => x.EndDate.Value)
.WithLocalizedMessage(() => CommonRes.Less_Than_Or_Equal_To, filters => CommonRes.Start_Date, filters => CommonRes.End_Date);
RuleFor(x => x.StartDate)
.GreaterThanOrEqualTo(x => x.AbsoluteStartDate)
.LessThanOrEqualTo(x => x.AbsoluteEndDate)
.WithLocalizedMessage(() => CommonRes.Between, filters => CommonRes.Start_Date, filters => filters.AbsoluteStartDate, filters => filters.AbsoluteEndDate);
Neither of the
LessThanOrEqualToorGreaterThanOrEqualTorules are supported by client side validation as explained in the documentation.This means that if you want to have client side validation for them you will need to write a custom
FluentValidationPropertyValidatorand implement theGetClientValidationRulesmethod which will allow you to register a custom adapter and implement the client side validation logic for it in javascript.If you are interested on how this could be achieved just ping me and I will provide an example.
Update
As request, I will try to show an example of how one could implement custom client side validation for the
LessThanOrEqualTorule. It is only a particular case with non-nullable dates. Writing such custom client side validator for all the possible case is of course possible but it will require significantly more efforts.So we start with a view model and a corresponding validator:
Then a controller:
and a view:
All this is standard stuff so far. It will work but without client validation.
The first step is to write the
FluentValidationPropertyValidator:which will be registered in
Application_Startwhen configuring our FluentValidation provider:And the last bit is the custom adapter on the client. So we add of course the 2 scripts to our page in order to enable unobtrusive client side validation:
and the custom adapter: