0

I have a simple form with a select element and an input element. Depending on the value in the select element i might or might not need the input element validated.

What i did is, i set the 'data-val' attribute on the input element to either 'true' or 'false' depending on what is selected in the select element. This part is working - i can inspect the value of the attribute in firebug and it does change according to the plan.

Now, in order to have the jquery unobtrusive validation to notice the change in the dom i call

$.validator.unobtrusive.parse(document)

I tried passing the form or the element, so i guess passing the document is actually just my latest attempt. I tried passing the jquery object of the form, as well as the dom-form element. I also tried validating only the input element calling

$.validator.unobtrusive.parseElement(myElement)

Again, i tried with the jquery object as well as the 'nekid dom element'.

What am i missing here?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101

2 Answers2

1

yeah, parse element does not work dynamically. Try this -it worked for me and a fair few others....

http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/

Xhalent
  • 3,914
  • 22
  • 21
  • Nasty news. Your extension seems to handle only new elements, not modified one? Also when changes are made to the unobtrusive library, modifications might be needed on your extension. Would it be possible to just remove/clear the validation on the form or single element and rerun the validation (minimize the extension logic?) – Per Hornshøj-Schierbeck Feb 02 '11 at 10:16
  • parse document will not run once the form's validator has been set. You COULD of course clear whatever you liked and start again, but you could imagine for a large dynamic form that could a less than elegant solution. Obvously the extension is coupled to the library in the same way the unobstrusive librayr is coupled to jquery.validate. My extension will not remove rules - the api already enables does this, but it certainly works for existing elements that have not been validated as yet - there is no concept of a 'new' element. – Xhalent Feb 02 '11 at 12:21
  • Ok - so if i had a rule on the input element and then want to remove/disable it because of a change in the select element. Would your extension script work for that? – Per Hornshøj-Schierbeck Feb 02 '11 at 12:35
0

Similar question (that i didn't find using search earlier) jquery.validate.unobtrusive not working with dynamic injected elements

There is a guy suggesting removing the validator object from the form, so calling parse will parse the whole form again.

Community
  • 1
  • 1
Per Hornshøj-Schierbeck
  • 15,097
  • 21
  • 80
  • 101