[development] Weird Javascript behavior after form_set_error on Drupal forms

David Thibault dthibault at esperion.com
Thu Dec 17 21:17:50 UTC 2009


OK, thanks!  I appreciate everyone's quick responses on this one.  You guys rock!
Dave

-----Original Message-----
From: development-bounces at drupal.org [mailto:development-bounces at drupal.org] On Behalf Of Steven Jones
Sent: Thursday, December 17, 2009 3:59 PM
To: development at drupal.org
Subject: Re: [development] Weird Javascript behavior after form_set_error on Drupal forms

David,

It's fairly standard practice to pop adding your js settings in a
simple function, and to use a simple static variable in that function
to ensure that the settings are only added once. Then you can then
call the function as many times as you want on a page load.

On Thursday, December 17, 2009, David Thibault <dthibault at esperion.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
> OK, this worked:
>
>
>
> Add_js_css.inc (a file with all the Drupal_add_css and Drupal_add_js
> calls in it).
>
>
>
> In hook_form_alter I did this:
>
>
>
> If ($node->type == 'purchase_order') {
>
>                 $form['#pre_render'][]
> = 'load_js_css';
>
> }
>
>
>
> In load_js_css() I did this:
>
> require_once('add_js_css.inc.');
>
> return $form;
>
>
>
> And that worked.
>
>
>
> Thanks, Scott!
>
> Dave
>
>
>
>
>
>
>
> From:
> development-bounces at drupal.org [mailto:development-bounces at drupal.org] On
> Behalf Of David Thibault
> Sent: Thursday, December 17, 2009 1:41 PM
> To: development at drupal.org
> Subject: Re: [development] Weird Javascript behavior after
> form_set_error on Drupal forms
>
>
>
>
>
>
>
> That almost works.  For some reason pre_render gets called
> twice and then it doubles up the calls to the Drupal_add_js (.settings)
> lines like this:
>
> drupal_add_js(
>
>     array('ldap_get_contact_details' =>
>
>       array("json_url" =>
> $json_url)), 'setting');
>
>
>
> My ajax calls use those settings to find the JSON urls to
> call.  If it gets called twice, then the settings array on the javascript
> side gets doubled up and breaks.  So then I moved those back to form alter
> (so they'd get called once) and left the other drupal_add_js and
> Drupal_add_css lines in the special my_load_js function.  Basically right
> now it's stuck at putting them in form_alter and they don't get
> called after the form_set_error returns, or put them in as pre_render options,
> and somehow that gets called twice in one page load.  I put a watchdog
> statement in my_load_js so that I could verify, and it does, in fact, get
> called twice per page load.
>
>
>
> Thoughts?
>
> Dave
>
>
>
>
>
> From:
> development-bounces at drupal.org [mailto:development-bounces at drupal.org] On
> Behalf Of Scott Reynolds
> Sent: Thursday, December 17, 2009 1:14 PM
> To: development at drupal.org
> Subject: Re: [development] Weird Javascript behavior after
> form_set_error on Drupal forms
>
>
>
>
>
> This is because on form error the form comes out of the
> cache table and skips fetching the form and all its _alter's. The
> 'best' practice I have found is to use a #pre_render function to
> drupal_add_js(). pre_render's are called always.
>
>
>
> <?php
>
>
>
>
>
> function my_form_alter() {
>
>
>
>
>
>   // add my elements
>
>
>
>
>
>   $form['#pre_render'][] = 'my_load_js';
>
>
>
>
>
> }
>
>
>
>
>
> ?>
>
>
>
>
>
>
>
>
>
>
>
> <?php
>
>
>
>
>
> function my_load_js($form) {
>
>
>
>
>
>   drupal_add_js();
>
>
>
>
>
>   return $form;
>
>
>
>
>
> }
>
>
>
>
>
> ?>
>
> --
> Scott Reynolds
>
>
>
>
>
>
>
>
>

-- 
Regards
Steven Jones
ComputerMinds ltd - Perfect Drupal Websites

Phone : 024 7666 7277
Mobile : 07702 131 576
Twitter : darthsteven
http://www.computerminds.co.uk




More information about the development mailing list