[development] Weird Javascript behavior after form_set_error on Drupal forms

David Thibault dthibault at esperion.com
Thu Dec 17 19:10:10 UTC 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20091217/16d30863/attachment.html 


More information about the development mailing list