Unobtrusive custom/conditional validation with Fluent Validation
Asked Answered
S

1

7

I'm looking for a way to implement unobtrusive custom validation for Fluent Validation. According to the documentation, it doesn't seem to indicate it supports unobtrusive validation.

Same applies to using conditional validation (When/Unless). I see in their MVC documentation, unobtrusive validation isn't supported with conditional and other complex validation:

Note that FluentValidation will also work with ASP.NET MVC's client-side validation, but not all rules are supported. For example, any rules defined using a condition (with When/Unless), custom validators, or calls to Must will not run on the client side. The following validators are supported on the client:

*NotNull/NotEmpty
*Matches (regex)
*InclusiveBetween (range)
*CreditCard
*Email
*EqualTo (cross-property equality comparison)
*Length

So has anybody figured out how to get this to work? If not, are there other validation options that provide better support for unobtrusive custom/complex validation?

Slone answered 8/6, 2012 at 19:0 Comment(2)
Could you provide a concrete example of what you are trying to achieve/validate? What's your exact scenario?Bart
Well, just using the When/Unless methods do not work unobtrusively, out of the box anyway. Even if I could find solution that worked for those, that would get me one step closer. Likely, if it works w/ conditionals like When/Unless, it would probably work w/ custom validation as well.Slone
T
1

I've plugged in FluentValidation to ASP.NET MVC 3 successfully, by following the Integration with ASP.NET MVC docs.

The simple way is to plug FluentValidation into the MVC Validation framework in Global.asax Application_Start() like this:

FluentValidationModelValidatorProvider.Configure();

Then you can decorate your POCO classes with attribute, specifying the validator they use.

[Validator(typeof(PersonValidator))]
public class Person {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
}

In my case, i didn't want to do that (use attributes), and actually needed to validate the same POCO class against different Validators, depending on the business rules.

If you want to select different validators that way, follow the docs on "custom validator factory with an IoC container". Create a class FluentMvcValidatorFactory subclassing ValidatorFactoryBase, which implements the interface IValidatorFactory. The custom validator factory can handle selecting the proper validator.

I was trying to get client side validation working (it did), but it seems to also plug into unobtrusive validation. My html is output looking like this:

<input type="text" value="" name="Email" id="Email" data-val-length-max="128" data-val-length="&amp;#39;Email&amp;#39; must be between 0 and 128 characters." data-val-email="&amp;#39;Email&amp;#39; is not a valid email address." data-val="true" class="text-box single-line">

As long as you plug it into MVC correctly, i think unobtrusive should work.

In practice, you should not depend entirely on client-side or unobtrusive validation, only use it for assisting the user during input. Ultimately the server needs to validate, but you can have your server-side code use the same FluentValidation validators.

Thornie answered 15/6, 2012 at 19:54 Comment(3)
Thanks for your post. I actually have standard validation working unobtrusively, the problem is getting the When/Unless conditional validations to work, or custom validation (where I create my own validation rules).Slone
I see - so you want to define the custom validation rules (conditionals) using FluentValidation, and have those rules converted to custom jquery.unobtrusive.validation rules for client side validation? That is what is not supported. So it would take a custom code to bridge the gap. How important is it to have the custom rules on the client side, not with a server post? Maybe it is overkill. Part of the reason to use FluentValidation is that you write less code - same validation rules on the client and server.Thornie
I agree. But there are many cases when validation of some fields is only required in some cases (i.e. require shipping address fields if the "Same as billing" checkbox is not checked). To me, this seems like a common requirement, and as you said, I don't want to have to write separate validation for this since the unobtrusive validation doesn't work for these When/Unless conditionals.Slone

© 2022 - 2024 — McMap. All rights reserved.