drupal_add_js() only adds the JS when there is no error message.
My code goes like this:
function ntcf_redo_order_form( &$form_state = array() ) {
global $base_path, $user;
$my_dir = drupal_get_path('module', 'ntcf_redo');
drupal_add_js("$my_dir/order.js", 'module', 'header', FALSE, TRUE, FALSE);
$form = array();
...
return $form;
}
If the validation function uses form_set_error() to display an error message, the error message is displayed, and the drupal_add_js() call does nothing, so while the error message is there, none of the usual behavior is. (Drupal's standard JS files are still included.)
Edit: using drupal_set_message() does not seem to cause a problem.
I tried adding the 3 later parameters to the drupal_add_js() call to tell it to not optimize it (don't combine it with other JS files). There is no mention of the file order.js in the HTML, and it makes no difference whether I use the last 4 parameters ('header', FALSE, TRUE, FALSE) or not.
In Admin/Performance, I turned off Optimize Javascript Files, and pretty much all caching, which also made no difference.
Extra Details:
I'm not sure if this makes a difference, but it wouldn't surprise me, so here goes:
What I'm doing here is a multi-part "wizard" form that allows the user to proceed forward and go back. Also, many of the pages use AJAX, so I need to do all the "required" field validation in the _submit function instead of letting Drupal do it automatically (since that makes a mess of AJAX). So, if there's a "required" field that's missing, the _submit() function sets an error message, and the form generation function generates the same form again (with the additional decoration resulting from the error message).
Also: this is off-topic, but it might help someone using Google: when doing a multi-page form that allows going backward, you MUST assign a weight to every item on the form, or else the fields tend to "wander" when you go backwards.
Any ideas?
Comments
Experiencing the same problem!
Could you please elaborate on this fix?
Yes, we are putting the JS directly into the form:
so... when the following function is invoked through validation:
Then absolutely ALL javascript functions are corrupted!!!!!!
Where do we work around this limitation with form_set_error corruption?
Cache ?
Hi,
Am guessing here, so correct me if am wrong.
Maybe retrieving the data from the cache (as your using a multi-step Form), will prevent drupal_get_form from running your initial form hook, and so running drupal_get_js.
Try,
$form['#cache'] = FALSE; Drupal 6
$form_state['#no_cache'] = TRUE; Drupal 7
$form_state['cache'] = FALSE; Drupal 7
But this may result in a data loss, submited thou every step.
To end with my feeling that this is caused by form cache, you can also try to add your js in $form[#pre_render] handler, this will be run whether your form is cahed or not.
Hope this help.
great feedback
Hello mirhk, I get amazed by the feedback from the Drupal community.
You guys are proactive with your solutions which leads me to assume you are smart!
thank you, that worked.
So, the rule is... when using form_set_error(...) in conjunction with drupal_add_js($js, 'inline');
then we need to associate the drupal add js with a pre rendering clause for the form (as opposed to included the drupal add js inline directly in the form function).
glad that worked
You are welcome, we are just giving back.
Thank you very much, after
Thank you very much, after all these years this comment helped me a lot!