7
votes

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?

1
Could you provide a concrete example of what you are trying to achieve/validate? What's your exact scenario?Darin Dimitrov
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.Jerad Rose

1 Answers

1
votes

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.