So the issue of ajax requests not being validated before submission was raised here: http://drupal.org/node/1486480 and the points about binding to click vs submit are very good.
That said, I think there is the potential here to support this within this module and thought I'd submit a patch version of what I've done and see what people think.
Basically, the change allows an extra 'validate_first' parameter in the ajax settings which will cause the clientside validation to abort the request on error.
'submit' => array(
'#type' => 'submit',
'#value' => t('Submit'),
'#ajax' => array(
'callback' => 'your_callback',
'validate_first' => TRUE,
),
),
There may also be a better way of doing this, rather than overwriting the beforeSubmit...
Comments
Comment #1
jelle_s(Slightly altered) patch committed to 7.x-dev (02497c5)
Comment #2
Davidovich commentedNice.
Comment #3
planctus commentedI get this in the firebug console:
I was trying to apply client side validation to a form being loaded through colorbox node, it is the forward module's form...
Thanks,
Da.
Comment #4
segi commentedI got a similar error in chrome
Uncaught TypeError: Cannot read property 'validate_first' of undefined
Comment #5
jelle_sThanks for the report, fixed in latest dev.
Comment #7
Saaj commentedSorry to reopen this issue, I've added:
When I load the page and submit the form the validation pops up fine. However but after the validation has run the form submit button no longer works.
When I changed the code in the module to the code that was submitted by Davidovich the form then worked fine even after validation.
Has anyone else encountered this same issue?
Comment #8
attiks commentedRelated issue #1848932: Usage with a views filter form
Comment #9
dustinyoder commentedI am currently running 7.x-1.36 and I have the same issue as Saaj in comment #7.
I enable clientside validation on a field in a form that I defined in my module. The submit button is using Ajax and I added the 'validate_first' =>TRUE line to the #ajax array on that form submit button.
The validation works the first time I press the button, then the submit button seems to lose it's ability to function. I can make the field value correct so it should pass validation but I click submit and no action seems to take place.
If I leave the field value valid from the beginning the form submits and the ajax loads properly, but then the submit button also stop working from then on.
If I disable the clientside validation on this form, everything works fine. I can submit the form multiple times over and over. As soon as I turn it on, the button only functions for one click.
Comment #10
blv commentedTry to manually insert the patch, add -
inside the
for (var ajax_el in Drupal.settings.ajax) { }function in clientside.validation.js... it should be in line 47... that's what i did and it works great on multiple submits...Comment #11
dubois commentedWorks for me after changing:
if (typeof Drupal.ajax[ajax_el] !== 'undefined' && Drupal.ajax[ajax_el].validate_first) {to:
if (typeof Drupal.ajax[ajax_el] !== 'undefined' || Drupal.ajax[ajax_el].validate_first) {Comment #12
jelle_sfixed in latest dev version
Tested as follows:
custom)Comment #13
jelle_sComment #15
paravibe commentedI have an error when on a page more than one ajax submit buttons.
I created patch. It's work for me.
Please review.
Comment #16
attiks commentedwhat difference does this make, you store ajax_el in ajax_elem, or am I missing something?
Comment #17
paravibe commentedYes, I store ajax_el in ajax_elem but only after
if (typeof Drupal.ajax[ajax_el] !== 'undefined' && Drupal.ajax[ajax_el].validate_first)this line.
So, when submit button is pressed only right ajax element is taken and not the last one.
Comment #18
attiks commentedthanks, I knew I missed something
Comment #20
brant.darilek commentedModified this patch to work with version 1.39.
Comment #21
brant.darilek commentedHere is a working patch.
Comment #22
luigisa#3 Works OK
if (Drupal.ajax[ajax_el].validate_first)
Comment #23
karolrybak commentedI've ended up using custom js code. I'm loading an form edit in modal. To prevent ajax submit you can use this js snippet: