Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP .Net MVC 3: Custom unobtrusive validation

I am trying to add a custom unobtrusive validation to my app. It does not seem to run the validation.

Here is my Attribute class:

public IEnumerable<ModelClientValidationRule> GetClientValidationRules(
    ModelMetadata metadata, ControllerContext context)
{
    yield return new ModelClientValidationRule
    {
        ErrorMessage = ErrorMessage,
        ValidationType = "custrequired"
    };
}

And my JavaScript:

$.validator.addMethod('custrequired', function(value, element, param) {
  return value && value !== '99:99' && value !== '9:99';
});

$.validator.unobtrusive.adapters.add('custrequired', null, function(options) {
  return options.messages['custrequired'] = options.message;
});
like image 860
Moon Avatar asked Feb 12 '12 02:02

Moon


People also ask

What does validator unobtrusive parse do?

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.

How ModelState IsValid works in MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What is custom validation in MVC?

This validation can be added for both the client side and the server side. You understand that decorating the properties in a model with an Attribute can make that property eligible for Validation. Some of the DataAnnotation used for validation are given below. Required. Specify a property as required.


1 Answers

There are plenty of things that can go wrong here, none involving the code you've posted:

  • How does your generated HTML for the <input> look? Does it include the data-val-custrequired attribute?

  • If not, did you remember to add the interface in the class declaration - IClientValidatable?

  • Did you remember to not add your custom validator inside a DOM ready handler? (i.e. don't use $(document).ready() or $(function() { ... })) - the validation should be set up before the document is ready.

ETA: The reason for the last one is that jquery.validate.unobtrusive does this:

$(function () {
    $jQval.unobtrusive.parse(document);
});

I.e., it parses the document for validation attributes, rules etc. on document ready. Since that script has to be loaded in order to register your adapters, your registration script would normally have to come after jquery.validate.unobtrusive.js. But that also makes any .ready handler you register be called after the "unobtrusive" one - by which point it's too late. I guess you could put your registration script before and then use .ready. Never tried it, and I've never seen it done.

Also, in this case, this should be enough to register the adapter:

$.validator.addMethod('custrequired', function(value, element, param) {
   return value && value !== '99:99' && value !== '9:99';
});

jQuery.validator.unobtrusive.adapters.addBool('custrequired');

It will still add any custom error message you may have, but since your validator isn't using additional parameters (like e.g. StringLength passes MaximumLength etc.), there's no need for a custom adapter.

like image 144
JimmiTh Avatar answered Sep 25 '22 12:09

JimmiTh