Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery unobtrusive validation ignores data-val-required message in MVC3

I have used the method described here to localize my data annotation messages and basically it is working fine in normal form posts. I can see the localized validation message on client side as well.

However, problem occurs when I have a partial view that has the input fields to be validated and is loaded using an ajax call. For some reason, I get the default validation message ("This field is required") instead of my localized message that is set in data-val-required attribute of the element.

I have verified the following:
1. I have included both "jquery.validate.min.js" and "jquery.validate.unobtrusive.min.js".
2. I checked the ajax response and it does contain data-val-required attribute with localized message.

I have already tried the following solutions:
1. I tried parsing the ajax response (that didn't work) as $.validator.unobtrusive.parse('form')
2. Parsing dynamic content for validation as described here. Still no solution.

I have a pressing deadline and this is troubling me.

like image 382
sprisoner Avatar asked Jun 14 '12 16:06

sprisoner


1 Answers

I think you already tried this: stackoverflow.com/questions/4406291/jquery-validate-unobtrusive-not-working-with-dynamic-injected-elements - the answer by Steve Lamb helped me the most.

Basically, you need to re-initialize the validator whenever you change the page content, as the validation code builds a list to track the controls it needs to validate only once, on page load...

I also seemed to have to add some extra code to go through and make sure the inputs were named correctly. Give me a moment and I'll dig the code up. - hmm, sorry, that code was part of a client app, and I can't post it. Not too sure it would have made a difference anyway...

like image 148
Tieson T. Avatar answered Nov 15 '22 18:11

Tieson T.