Im having some trouble enabling on-the-fly the client side validation for a custom form I insert in the page dinamycally (ajax request).
My workflow is somehow this:
function mymodule_menu(){
return array(
'mymodule/load-form' => array(
'page callback' => '_mymodule_load_ajax_form',
'access callback' => true
)
);
}
function _mymodule_load_ajax_form(){
$form = drupal_get_form('mymodule_form_test');
$html = drupal_render($form);
drupal_output_json(array('status' => true, 'html' => $html));
}
In my tpl file, I have a button that when clicked call ana ajax request to mymodule/load-form, and insert the form html into the page (the compelte code is a bit more complex, has the server-side validation and stuff)
My problem is that I cant enable (and then, disable when the form get submitted via ajax) the Client Side validation.
I tryed, in my ajax success callback, to dinamycally load all the client side js files:
/sites/all/modules/clientside_validation/jquery-validate/jquery.validate.js
/sites/all/modules/clientside_validation/clientside_validation.ie8.js
/sites/all/modules/clientside_validation/clientside_validation_html5/clientside_validation_html5.js
/sites/all/modules/clientside_validation/clientside_validation.js
I also get the js settings that clientside use and add them to Drupal.settings; After a lot of try, the Drupal.settings is identical to the one you will get rendering the form normally (and not via ajax), but still I cant understand why client side validation does not works.
Any idea?
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | clientsideValidation_DinamycForm-2136509-21-7.x-2.x.patch | 3.19 KB | khiminrm |
| #19 | clientsideValidation_DinamycForm-2136509-19-7.x-2.x.patch | 2.82 KB | hugronaphor |
Comments
Comment #1
jelle_sThis seems like a "non-Drupal" way to load a page through ajax.
You really should be doing something more like this: (untested code)
Comment #2
strae commented@Jelle_S, thanks for the answer, youre totally right..
I'll try to convert my code in "drupal style"
Comment #3
strae commentedComment #4
strae commentedAfter Jelle_S answer, i tryed to use the Drupal ajax framework to load the form, and I guess I found a bug in clientside validation: when I load the form via ajax, I always get the error "TypeError: j is undefined" in jquery.js?v=1.4.4 (row 33).
Thats happen just after the loading of clientside_validation.js: http://imgur.com/TwqvSmJ
I also tryed to load manually the clientside_validation js in the page, but I get the same error (perhaps somethig needs to be initialized before load them?)
If i disable clientside_validation modules (currently im using fapi and form submodules) the ajax workflow works perfetcly.
This is the simpliest code to reproduce the error:
Comment #5
strae commentedComment #6
strae commentedComment #7
jelle_sCan you try the latest dev again (pull from git or wait 12 hours for d.o to sync). It works for me now with the example code you provided.
Comment #8
strae commentedJelle_S, i'll try tomorrow and tell if it works ;)
Comment #9
p.parshin commentedI had the same problem and latest dev-version helped me.
Thanks.
Comment #10
jelle_s@Strae: As this fixed @p.parshin's problem, can I assume it fixed yours as well?
Comment #11
strae commentedJelle_S ys, just tryed and now works, sorry but i've been very busy
Comment #12
jelle_sNo problem, thanks for reporting back!
Comment #14
k.skarlatos commentedSorry for reopening, please tell me if this needs to be in a new issue:
I have a webform, that uses modal forms and does not use clientside validation in that specific form. I get Internal Server Errors after this commit (git bisect results):
I also get this message:
Comment #15
k.skarlatos commentedComment #17
jelle_sCan you try version 1.41? This should be fixed.
Comment #18
andriyun commentedCommited patch #16 work for full form ajax update.
jquery validate bind to new form id and all work fine.
But if we update part of form elements via ajax validation still don't work
Comment #19
hugronaphor commented@andriyun is right, if you update a part of your form the the validation will not be triggered because the JS settings are not going to be available for the original Form ID.
Uploading the patch for version 7.x-2.x which is going to ensure the settings are available for the original Form ID.
Comment #20
hugronaphor commentedComment #21
khiminrm commentedI've faced with error by using patch from #19:
clientside_validation.js?qgno6y:254 Uncaught TypeError: Cannot read property 'errorElement' of undefinedI've updated patch by adding
clientside_validation_keep_js_settings(array($form_id => $settings['clientsideValidation']['forms'][$form_id]));at the end of function_clientside_validation_add_general_settings()