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@drupal.org [mailto:development-bounces@drupal.org] On
Behalf Of Scott Reynolds
Sent: Thursday, December 17, 2009 1:14 PM
To: development@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