I am using a simple jquery script to show and hide various controls based on the value of a check box on the same form. For example, if the user checks the is_customer check box, I want to reveal a number of customer categorization options. If the user unchecks the check box, I want to hide those options.

The script works most of the time. However, I just added a hook_validate implementation. Now when the validation fails, the form is redisplayed but my javascript file is not included. I know because I viewed the page source before and after the validation.

I tried loading the script in hook_form and in hook_form_alter (using drupal_add_js) and neither solved my problem. Is there somewhere else I can add the form which will get called when hook_validate fails?

I was hoping to avoid spending the afternoon stepping through the code trying to identify the problem. Can you help? Thanks!

Comments

zbricoleur’s picture

You just have to put the same drupal_add_js in the validate function as well.

chrisshattuck’s picture

I'd be curious if your javascript was throwing an error. If you had it in hook_form_alter, then it should be added in whenever the form is rendered, as long as there's no conditionals surrounding it that might change on the form validation. Do you see any errors in Firebug?

Cheers,
Chris

Build a Module.com - The definitive video guide to Drupal development

Learn virtually any aspect of Drupal on BuildAModule, where I've recorded over 2200 video tutorials.

zbricoleur’s picture

nhunter’s picture

Thanks for the replies and the link. I'm surprised I did not come across the previous material myself. Thanks again.