Closed (fixed)
Project:
Drupal core
Version:
6.x-dev
Component:
forms system
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
16 Oct 2008 at 20:08 UTC
Updated:
11 Jan 2011 at 06:06 UTC
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
Comment #1
skilip commenteddrupal_add_css() also doesn't function anymore after page refresh with validation errors
Comment #2
skilip commentedThis is my code:
I've already tried to set the cache argument for drupal_add_js to FALSE, but no success.
Comment #3
markDrupal commentedI 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.
Comment #4
ivan zugec commentedThis 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.
Comment #5
gábor hojtsyhook_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()).
Comment #6
skilip commentedI've managed to solve this by adding a callback to '#after_build' in which the js and css files are added.
Comment #7
damien tournoud commentedThe 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.
Comment #8
kenorb commentedThanks.
Comment #9
earthangelconsulting commentedre: #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
Comment #10
rajarju commentedhey,
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