I am trying to validate if a check box is checked on the client using FluentValidation. I can’t figure it our for the life of me.
Can it be done using unobtrusive validation?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s assume that you have the following model:
with the following validator:
and a controller:
with a corresponding view:
and in
Global.asaxyou have registered the fluent validation model validator provider:So far we have server side validation up and running fine. That’s good. That’s always the first part that we must setup. I have seen people focusing too much on doing client side validation that they forget doing server side validation and when you disable javascript (or even worse if you stumble upon a user with bad intentions), well, bad things happen.
So far we are confident because we know that even if something gets screwed up on the client our domain is protected with server side validation.
So let’s now take care for the client validation. Out of the box FluentValidation.NET supports automatic client validation for the
EqualTovalidator but when comparing against another property value which is the equivalent of the[Compare]data annotation.But in our case we are comparing against a fixed value. So we don’t get client side vaildation out of the box. And when we don’t get something out of the box, we need to put it in the box.
So we start by defining a custom FluentValidationPropertyValidator:
that we are going to register in
Application_Start:So far we have associated our custom FluentValidationPropertyValidator with the EqualValidator.
The last part is to write a custom adapter:
And that’s pretty much it. All that’s left is to include the client scripts: