Austin Smith

Dynamic Form Altering Using #ahah, The Right Way

Mon, 01/26/2009 - 22:05

Lately I've been wrapped up pretty seriously helping dww migrate Project Issue Tracking to Drupal 6 as part of the Drupal.org sprint in Boston (I'll be joining them on Wednesday). Today I continued work on this issue, specifically converting an old hard coded AHAH routine to use #ahah, eliminating custom security checks and an entire custom Javascript file, not to mention fixing the sheer brokenness of the thing in Drupal 6. I won't walk you through the whole #ahah process; you can get that here. All I want to do is use #ahah to change the structure of a form--and do it it right. Here's what I learned.

So here's the scenario: When replying to an issue, users can change the parent project of that issue. When they select a new project in the dropdown, the version, assigned, and components elements update to display the relevant data for the newly selected project.

And here's the problem with that scenario: The form structure is cached, so even when we changed the elements on the page using the old Drupal 5 way (with the aforementioned custom Javascript), crazy validation errors occured because the form displayed to the user was not the form in the cache. So we decided to abandon the custom approach entirely for Drupal's beautiful #ahah support.

What this boils down to is reflected in the title of this article; using #ahah for a helper function rather than actually submitting a form with it. We need to be able to use the results of the #ahah callback in our form structure so that when the form is actually submitted via plain old POST, the submit and validate functions are none the wiser.

The aforementioned wiki page on Drupal.org was a very helpful starting point, but I needed to do a little more. Here are the issues:

  1. The form's #submit callbacks need not to fire. There's a simple way to do that, set $form_state['rebuild'] = TRUE in your #ahah callback.
  2. In the #ahah handler drupal_rebuild_form sets $form['#action'] to be the current page. If you're actually submitting the form using #ahah, no big deal. But we're not, we're just altering it. So we needed to set $form['#action'] ourselves in our hook_form_alter function. The end result is not pretty--you get a page full of JSON grossness.
  3. The project_issue #validate function needs not to fire in the #ahah callback. So I set an arbitrary flag in the #ahah callback in $form_state (can be anything). I checked for its presence at the beginning of the validate function; if it's there, just return.
  4. We also agreed to make a concession to the #ahah system: We moved the assigned dropdown to the same row as component, version, and project because it can also be updated when project changes. It was either this, custom Javascript, or two callbacks.

So here's the replicable part of the #ahah handler. Note where it varies from the wiki example. If you want the full code, check the patches posted against this issue.

$form_state = array('storage' => NULL, 'submitted' => FALSE, 'rebuild' => TRUE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
 
// Skip validation in project issue.
$form_state['project_issue_ahah'] = TRUE;
drupal_process_form($form_id, $form, $form_state);
// everything beyond here just alters the form and then renders the desired chunk.

And here are the relevant snips of hook_form_alter:

// Set the right #action
if (!empty($form['pid']['#value'])) {
  $form['#action'] = url('comment/reply/' . $nid . '/' . $form['pid']['#value']);
}
else {
  $form['#action'] = url('comment/reply/' . $nid);
}
 
// Snip
 
// The #ahah enabled dropdown
$form['project_info']['pid'] = array(
  '#type' => 'select',
  '#title' => t('Project'),
  '#default_value' => $node->project_issue['pid'],
  '#options' => $projects,
  '#required' => TRUE,
  '#ahah' => array(
    'path' => 'project/issues/update_project',
    'wrapper' => 'project-info-wrapper',
    'event' => 'change',
  ),
);
 
// Snip
 
// Need this to have a predictable updatable div.
$form['project_info']['#prefix'] = '<div id="project-info-wrapper" class="inline-options">';
$form['project_info']['#suffix'] = '</div>';

You get a few cool freebies out of this: standardization, security (via form build ID; if the user didn't see the form, they can't change it), less custom code overall, and best of all: an awesome AJAX spinner!

Thanks to chx for the initial explanation and many subsequent clarifications, and to dww for being so patient! Hope this helps someone; I'm quick to correct errors, so if there are any, please say so!

aha helper module

Submitted by moshe weitzman (not verified) on Tue, 01/27/2009 - 11:12.

Thanks for the tip, will give

Submitted by Tom (not verified) on Tue, 03/08/2011 - 07:59.

Thanks for the tip, will give it a go :) inkasso

I appreciate it as well. Will

Submitted by actron cp9180 (not verified) on Thu, 03/17/2011 - 06:31.

I appreciate it as well. Will come in handy. Actron cp9580

Well i think this is really

Submitted by Luke John (not verified) on Wed, 08/10/2011 - 04:08.

Well i think this is really helpful for people like me who are new to this field and want to enhance their knowledge about it.
custom logo design

That's why I appreciate the

Submitted by Roger Seldis (not verified) on Sun, 10/30/2011 - 12:14.

That's why I appreciate the author of this post that who unless the knowledge about the above topic added skill that is the general reader like me possible to fully understand this text. template web

Great

Submitted by sturridge (not verified) on Tue, 11/29/2011 - 05:17.

Great stuff from you, man. Ive read your stuff before and you're just too awesome. I adore what you've came, love what you're saying and the way you say it. You are making it entertaining and you still manage to keep it smart. I cant wait to read more from you. This is really an excellent blog.
service@Restaurants

I like this post very much,

Submitted by Julia alrn (not verified) on Sat, 12/03/2011 - 01:21.

I like this post very much, You have defined it very simply for so I understand what you say, In this post your writing level is also excellent to us. This is great issue you have done on this topic really very well.
www.bellspharmacy.com

Good post, thanks for using

Submitted by Anonymous (not verified) on Tue, 12/06/2011 - 13:23.

Good post, thanks for using this web space to discuss Dynamic Form Altering Using #ahah.
Fortunately this topic is also presented in your blog, assuring a decent coverage.
Keep up the good work !
glutamina

Very helpful information

Submitted by Isabella Taylor (not verified) on Thu, 12/15/2011 - 09:26.

This was really so very interesting and helpful article for me to read. I have enjoyed all of this information so much. Thanks - Aquawebkatalog

re

Submitted by Suedallin (not verified) on Sun, 01/08/2012 - 06:21.

Just what I was looking for. Thanks for sharing, your blog is great, I bookmarked. Online holiday booking in Europe - rent villa for your summer escape.

Thanks for taking the time to

Submitted by Bruno49 (not verified) on Wed, 01/11/2012 - 18:02.

Thanks for taking the time to discuss this I feel strongly about it and love learning more on this topic. If possible as you gain expertise would you mind updating your blog with more information? as it is extremely helpful for me.
twitter backgrounds

I wanted to draft you the

Submitted by cara menurunkan berat badan (not verified) on Fri, 01/20/2012 - 08:22.

I wanted to draft you the little note to finally say thank you again over the stunning principles you have shown in this case. This has been certainly extremely generous with people like you to offer easily all that some people would've made available as an e-book to generate some money for themselves, even more so considering the fact that you could possibly have done it in case you wanted. These solutions additionally acted to provide a good way to know that some people have similar desire much like my very own to understand a little more regarding this problem. I'm certain there are some more fun instances up front for individuals who scan through your blog post.

Very interesting and

Submitted by Anonymous (not verified) on Thu, 02/02/2012 - 23:12.

Very interesting and insightful.
radyo dinle

nice one

Submitted by ansari12 (not verified) on Sat, 02/04/2012 - 02:02.

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post copper kitchen sinks

RE

Submitted by Adam Nasha (not verified) on Thu, 01/26/2012 - 04:36.

That's why I appreciate the author of this post that who unless the knowledge about the above topic added skill that is the general reader like me possible to fully understand this text.
web hosting dubai

Good one..

Submitted by sikha (not verified) on Wed, 02/01/2012 - 04:32.

Its a good way to describe about this concept,I hope people will have taken more information here.Thanku so much!!
günstig Urlaub machen

Nice Source Of Information

Submitted by christopherdoylelam (not verified) on Mon, 01/09/2012 - 08:34.

Blogs are good for every one where we get lots of information for any topics nice job keep it up !!! Analysis Essay

Reading the same blog

Submitted by jessicaale (not verified) on Wed, 12/14/2011 - 23:07.

Reading the same blog repeatedly it was boring,when i read your blog it was amazing and very interesting the way of narration is awesome,am glad that i found such a beautiful post.Thanks a lot for sharing it,i hope i will see more better post than this and i will be a frequent visitor from now onwards.Keep it up and keep rocking.
Cell Phone Spy

yes that is

Submitted by Januska1986 (not verified) on Mon, 12/26/2011 - 16:37.

That's why I appreciate the author of this post that who unless the knowledge about the above topic added skill that is the general reader like me possible to fully understand this text. Cam porno

RE: Well i think this is really

Submitted by Nicky85 (not verified) on Fri, 12/30/2011 - 15:14.

I agree 100% with Luke, its really helpfull to find exact details about the issue. But its not really easy to find the correct one, because i see every time here and there informations who write other things than the other one.
Handy orten

Great

Submitted by Steve (not verified) on Fri, 02/03/2012 - 14:30.

I've been trying to do that exact thing with my form, thanks for this great information! | Things to do in Cayman Islands

nice offer

Submitted by Anonymous (not verified) on Wed, 01/25/2012 - 01:35.

The post is written in a very good manner and it entails many useful information for me. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement the concept.
Twinksgal | Maxfightdm | Spirit | PremierCD | Danye-MX

Drug:)

Submitted by Anonymous (not verified) on Fri, 01/27/2012 - 07:00.

Tadacip 20 acts record for a long time - up to one and a half days! Such an effect as tadacip 20 mg not gives any other drug for prevention of erectile dysfunction! Do you want to live a full life? Buy tadacip 20 mg !

Drug:)

Submitted by Anonymous (not verified) on Fri, 01/27/2012 - 07:01.

Tadacip is an exact equivalent of Cialis - the original drug, used for treating erectile dysfunction. You want a long and rich sexual life? We offer you to buy tadacip online at affordable prices. We provide the opportunity to buy tadacip 20 with shipping. Buy tadacip from us, confidentiality and efficiency of our work you will be surprised. If you still doubt, where to buy tadacip 20 mg - read tadacip review thankful customers.

Drug:)

Submitted by Anonymous (not verified) on Fri, 01/27/2012 - 07:01.

Silagra is the exact counterpart of Viagra on chemical properties, effects and other pharmacological parameters, but it has an obvious advantage is silagra 100 cheaper. If you had a question where to buy silagra necessarily address to us. We have lowest prices on silagra online at very high quality of the goods.

Drug:)

Submitted by Anonymous (not verified) on Fri, 01/27/2012 - 07:02.

We offer you to buy suhagra - the next working out of company Cipla, the famous Indian pharmaceutical manufacture. Evaluate the pros suhagra 100mg or suhagra 100 you can right now. Make a reservation suhagra online in our pharmacy, and in the shortest possible time Suhagra will be delivered to any address.

Good One...

Submitted by srkkhan (not verified) on Fri, 02/03/2012 - 04:37.

Interesting post and thanks for sharing. Some things in here I have not thought about before.Thanks for making such a cool post which is really very well written.will be referring a lot of friends about this.Keep blogging.. copper farmhouse sinks

Good Post

Submitted by williamfitzstephen (not verified) on Mon, 01/09/2012 - 08:29.

Just look at the improvement in the quality of life and the wealth of normal working people since the start of the industrial revolution and you will see the contribution that technical progress has made to all of our lives.Essay Help

Thx

Submitted by Fridalein (not verified) on Mon, 04/04/2011 - 08:20.

Hi

thx for your comment on my homepage in the last days.

Greets

proszę

Submitted by Anonymous (not verified) on Wed, 01/25/2012 - 01:38.

It is very encouraging to go through the post for it contains information about these interesting feature. It is a useful tutorial .
Online Tips | Planete

wow

Submitted by michaeljii (not verified) on Mon, 07/25/2011 - 05:55.

The particulars and exact recommendation are insurance specifically what I was wanting. I’ve book marked and will definitely be returning.business insurance Thanks for the information in this blog.The posting in this site is very cool and also car insurance interesting.I had read the entire blog and I came to know many things which dental insurance I don't know before.I am sure that the visitors who visit this site will also be enjoying reading the posts.Keep it up.Waiting for new posts from you to be posted in this site.
350-001\642-902\646-205\220-702\350-018\642-982\JN0-660\HP0-Y30\350-050\642-627
Regards,
MichaeL || NYC.USA.

Thanks for your marvelous

Submitted by seplouge (not verified) on Wed, 12/28/2011 - 01:52.

Thanks for your marvelous posting! I actually enjoyed reading it, you will be a great author.I will ensure that I bookmark your blog and will come back in the foreseeable future. I want to encourage that you continue your great job, have a nice weekend.
the fastest cars

Bravo!

Submitted by arranbruno (not verified) on Wed, 01/11/2012 - 05:11.

This is probably one of the best mentions of this topic I’ve seen in quite a while. It’s obvious that your knowledge of the subject is deep and this made for a very interesting read.Writing Essays For University

dedicated hosting

Submitted by ajit kumar (not verified) on Sun, 07/24/2011 - 13:19.

plz click herededicated hosting

good article

Submitted by danish (not verified) on Tue, 09/27/2011 - 01:42.

This is one of the most informative information I've read. It really helps a lot. Thanks for sharing this and teaching soem of your Idea's.
Personsøk

I've been searching for this

Submitted by GaryT (not verified) on Sat, 10/29/2011 - 11:32.

I've been searching for this code for like 3 hours now. Thanks for posting a tutorial in this you really helped me a lot. I've been working on this for a couple of days now and I think that I am 80% before I finish programming it (a big thanks to your tutorial by the way).

RE

Submitted by Adam Nasha (not verified) on Thu, 01/26/2012 - 04:40.

Just look at the improvement in the quality of life and the wealth of normal working people since the start of the industrial revolution and you will see the contribution that technical progress has made to all of our lives.

web design oman | web design muscat | web design saudi

I've been wondering how to do

Submitted by Anonymous (not verified) on Mon, 10/31/2011 - 03:59.

I've been wondering how to do this, and now someone has been kind enough to show me how the whole thing is done. THis is brilliant, I can't wait to test it out myself! 84 inch shower curtain If you were on reddit, this would get upvoted to the front page :)

Well

Submitted by Ava James (not verified) on Fri, 11/04/2011 - 09:44.

I am a newbie and found this blog a good resource of programming help, looking forward for more good posts!
logo design service

Thank you for sharing this

Submitted by Anonymous (not verified) on Wed, 01/11/2012 - 01:14.

Thank you for sharing this wonderful post.radyo dinle

ahah the right way

Submitted by Colin (not verified) on Fri, 02/03/2012 - 13:59.

Really useful information thanks non peroxide teeth whitening

Thanks for the code i was

Submitted by frride (not verified) on Sat, 02/04/2012 - 10:47.

Thanks for the code i was search for this for a very long time watch live cricket thanks again

drupal project

Submitted by Sharron (not verified) on Sat, 02/04/2012 - 10:52.

Hey this links is really useful, Cock Rings

Ouch

Submitted by Steven Jones (not verified) on Fri, 02/06/2009 - 12:26.

Look at the 'Posted in' line underneath the headline. Ouch.

Really wonderful info can be

Submitted by bangla song (not verified) on Thu, 01/13/2011 - 12:10.

Really wonderful info can be found on blog .
I like this blog very much, Its a very nice spot to read and find information.
bangla movie
bangla gaan

I'm surprised at the way in

Submitted by amgualb (not verified) on Thu, 06/16/2011 - 10:10.

I'm surprised at the way in which this conversation has been conducted. Professional disagreement is fine, but there is a time and place. Does anyone agree?
jeux de mario
Juegos de Ben 10

Ouch.? Sincerely, Kopilka and

Submitted by Anonymous (not verified) on Sun, 02/06/2011 - 08:51.

Ouch.?
Sincerely, Kopilka and Sildenafil Team.

I have to agreed that this is

Submitted by Shandie Moe (not verified) on Tue, 01/03/2012 - 06:00.

I have to agreed that this is a tedious job moving from a working environment to the next phase. Hope all is well documented. SI joint pain.

Nice Blog

Submitted by Amanda (not verified) on Wed, 03/16/2011 - 01:15.

Search
Thank you so much for your work on keeping the site together and keeping these flowing.

hamel insurance

Submitted by mcfarlin insurance (not verified) on Thu, 10/21/2010 - 23:59.

I am curious just what Tim will change with this???

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <p>
  • Lines and paragraphs break automatically.

More information about formatting options