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?
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?
I am coding in ASP.NET MVC5 and Darin's code produces a javascript error on the lines that reference value.ToLowerCase() when a checkbox is involved. Another issue is that this code invalidates the client side equality comparison between two properties. It only seems to work when comparing against a literal value...That may have been his intent, but I need it to work for both situations:
Here's one possible workaround, that involves only two changes to Darin's answer:
First, I updated the javascript function with the following.
Secondly, I updated EqualToValueFluentValidationPropertyValidator with the following:
This code was copied from the
EqualToFluentValidationPropertyValidator
internal class in the fluentvalidation source, and I added Darin's logic after the else. This allows the client-side validation to work for property comparisons as well as value comparisons...I'm not sure if this is a great approach since you're basically overriding the built-in equality validator and it may break in future releases of fluent validation....but Darin's answer has the same issue.There might be better ways to handle this. If somebody knows of a way to directly include the logic from the internal
EqualToFluentValidationPropertyValidator
class, then I'd love to hear it.I like the Darin Dimitrov's answer, but if you want to do it quickly, here is my alternative way.
Create an additional property in your model, e.g.:
and set its value to
true
in the model's contructor.Use it in your view to save the value across the requests:
Now add a validation rule like this:
That validation is supported by the unobtrusive validator out-of-the-box.
it's based on @cryss answer
you don't need to use additional property
Let's assume that you have the following model:
with the following validator:
and a controller:
with a corresponding view:
and in
Global.asax
you 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
EqualTo
validator 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: