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
*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?
Fluent Validation is a validation library for . NET, used for building strongly typed validation rules for business objects. Fluent validation is one way of setting up dedicated validator objects, that you would use when you want to treat validation logic as separate from business logic.
An unobtrusive validation in jQuery is a set of ASP.Net MVC HTML helper extensions.By using jQuery Validation data attributes along with HTML 5 data attributes, you can perform validation to the client-side.
validator. unobtrusive. parse(selector) method to force parsing. This method parses all the HTML elements in the specified selector and looks for input elements decorated with the [data-val=true] attribute value and enables validation according to the data-val-* attribute values.
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="&#39;Email&#39; must be between 0 and 128 characters." data-val-email="&#39;Email&#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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With