If I add drupal_add_js in a form_alter I adds the scripts like it should when the form is in it's normal state. However, after a submition of the form, and the form did not pass the validation, the scripts aren't present any more.

Comments

skilip’s picture

Priority: Normal » Critical
Status: Postponed (maintainer needs more info) » Active

drupal_add_css() also doesn't function anymore after page refresh with validation errors

skilip’s picture

Project: API » Drupal core
Version: master » 6.x-dev
Component: Code » forms system

This is my code:

/**
 * Implementation of hook_form_alter().
 */
function swfupload_form_alter(&$form, $form_state, $form_id) {
  $path = drupal_get_path('module', 'swfupload');
  drupal_add_css("$path/swfupload.css");
  drupal_add_js("$path/swfupload.src.js");
}

I've already tried to set the cache argument for drupal_add_js to FALSE, but no success.

markDrupal’s picture

I found out if you add the identical lines to the _form_validate hook you can have your js and css load after a validation error.

/**
* Implementation of hook_form_alter().
*/
function swfupload_form_alter(&$form, $form_state, $form_id) {
  $path = drupal_get_path('module', 'swfupload');
  drupal_add_css("$path/swfupload.css");
  drupal_add_js("$path/swfupload.src.js");
}

/**
* Implementation of hook_form_validate().
*/
function swfupload_form_validate(&$form, $form_state, $form_id) {
  $path = drupal_get_path('module', 'swfupload');
  drupal_add_css("$path/swfupload.css");
  drupal_add_js("$path/swfupload.src.js");
}
ivan zugec’s picture

This also happens in hook_form. If you create a node type via a module and use drupal_add_js in hook_form, the JavaScript code will to be included if the form doesn't pass validation.

gábor hojtsy’s picture

Category: bug » support
Priority: Critical » Normal
Status: Active » Closed (fixed)

hook_form_alter() will not be called again if the *form* is cached for efficiency. Set the form to be non-cacheable to get hook_form_alter() called again. Or use the other creative ways mentioned here to add JS to forms being altered (you can also add it based on paths and other tricks in hook_init() and hook_help()).

skilip’s picture

I've managed to solve this by adding a callback to '#after_build' in which the js and css files are added.

damien tournoud’s picture

The best way is to create a theme function and add CSS / JS files there. It makes little sense to use a #build_after callback for this.

kenorb’s picture

Thanks.

earthangelconsulting’s picture

re: #7

well... if it's javascript or css that is used in all pages, or even just in a subset of them (but won't cause errors or unforeseen behaviour by including in all of them) then yes, the theme function is the place to do it.

but my recommendation would be: if you have LARGE javascript files that are only needed on certain forms, and/or if you are using drupal_add_js to add chunks of inline javascript that you want executed when that form loads... then i have found that the easiest solution is to do what MarkDrupal suggested in #3. you can even build a helper function that loads all your css for that form (even including parameters and conditionals if needed) and call that function from both _form_alter (or _form, if a node module) and from your validation function.

speaking of which, for some interesting discussion on validation functions for node modules, and why the _form_validate hook is NOT always the right place to implement them (particularly if you are actually changing values in your validation, not just returning a yes or no... see http://api.drupal.org/api/function/form_set_value/6 and to some degree, http://drupal.org/node/160160

cheers
Fish
GoatVirus Technologies

rajarju’s picture

Issue tags: +form validation

hey,

What i have seen is to use #after_build

This function is called even when there is a form validation error!

Remember you have to return $form_element even if you are not altering it

Cheers