0

Hi I am loading a form and using unobtrusive clientside validation extended with the foolproof nuget package. This all works well but now I am trying to load parts of the form with ajax.

Once these parts have been loaded in using ajax, the validation isn't bound to the new fields.

For the normal validation, I am able to rebind it using

    $.validator.unobtrusive.parse('#form');

Is there a similar method for rebinding them foolproof validation?

Pete
  • 57,112
  • 28
  • 117
  • 166
  • 1
    Everything's foolproof until you turn off JavaScript. – George Stocker Oct 30 '15 at 14:51
  • javascript isn't being turned off – Pete Oct 30 '15 at 14:53
  • 1
    The same code will reparse foolproof validation (but before calling `$.validator.unobtrusive.parse('#form');`, you should be calling $('#form').data('validator', null); –  Oct 30 '15 at 22:05
  • Hi @Stephen, just got back to work and tried out your suggestion and it seems to have done the trick, thanks. If you'd like to post it as an answer I'll accept it – Pete Nov 16 '15 at 13:08

1 Answers1

3

foolproof adds client side validation in the same way that MVC's validation attributes do, by defining a rule to be added to jQuery validation using the $.validator.addMethod() and $.validator.unobtrusive.adapters.add() functions, so parsing the validator will also work for foolproof validation attributes.

Note that before reparsing the validator, you should reset the validator to null

var form = $('form');
form.data('validator', null);
$.validator.unobtrusive.parse(form);
  • OMG thank you! This is also helpful if you are using other plugins that mangle the DOM. I am using a module called "steps" that quietly clones the DOM but the validator still had references to the original controls so that a password equalTo was always comparing to the original control (with a blank value). Very annoying! – Kris Oye Jun 27 '16 at 02:53