[development] Weird Javascript behavior after form_set_error on Drupal forms

David Thibault dthibault at esperion.com
Thu Dec 17 18:41:07 UTC 2009

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:
    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.


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.
function my_form_alter() {
  // add my elements
  $form['#pre_render'][] = 'my_load_js';

function my_load_js($form) {
  return $form;

Scott Reynolds
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20091217/201b7ef4/attachment-0001.html 

More information about the development mailing list