Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually bind JQuery validation after Ajax request

I'm requesting an ASP.net MVC view into a live box and the view contains form fields that have been marked up with attributes to be used by JQuery's unobtrusive validators plug-in.

The client script is not however working and my theory is that its because the validation framework is only being triggered on page load which has long since passed by the time the MVC view has been loaded into the live box.

Thus how can I let the validation framework know that it has new form fields to fix up?

Cheers, Ian.

like image 394
Ian Warburton Avatar asked Jun 07 '11 17:06

Ian Warburton


3 Answers

var $form = $("form");

$form.unbind();
$form.data("validator", null);

$.validator.unobtrusive.parse(document);
// Re add validation with changes
$form.validate($form.data("unobtrusiveValidation").options);
like image 143
dfortun Avatar answered Nov 05 '22 19:11

dfortun


You may take a look at the following blog post. And here's another one.

like image 42
Darin Dimitrov Avatar answered Nov 05 '22 19:11

Darin Dimitrov


Another option, rather trick, which worked for me. Just add following line in the beginning of the partial view which is being returned by ajax call

this.ViewContext.FormContext = new FormContext(); 

Reference

like image 1
bjan Avatar answered Nov 05 '22 20:11

bjan