By jrbrown1977@gmail.com on
Hello,
I have a form that when a selection is made a new selection is shown underneath with options that go with the selection you just made. I want to use AHAH with this, but am scratching my head on how to use AHAH return a $form item.
Does drupal store your form internally int the database, or does it handle it directly through the POST?
Should I create the select list but have a prefix and suffix to hide it, then have the AHAH take that who section and just dump out the HTML that drupal generates, so it matches.
Thanks,
J
Comments
Unfortunately, AHAH is a HUGE
Unfortunately, AHAH is a HUGE pain in the butt to figure out. It is not intuitive at all.
I spent weeks trying to figure it out, and ended up getting it to work on a few examples, before I found the AHAH Helper Module. It makes everything so much easier. I fully recommend it, it will save you hours of headaches, and by the looks of it, it will be incorporated into Drupal 7 core.
Contact me to contract me for D7 -> D10/11 migrations.
I'm looking at the helper
I'm looking at the helper module, and am a little confused. You specify which portion of the form you want re-rendered, but how would I add something. How exactly does it work, does it call the whole form function again, and ignore every form item except for what was specifically defined in the AHAH callback
In my case, I want to add a select box with a list of options based on a different select box selection.
Thanks,
J
When writing your _form
When writing your _form function, you can pass the value of $form_state. This will be empty on the first run, but will contain values upon subsequent runs. You can set elements to be added based on that. For example
When the form is first loaded, $form_state['values'] is empty. So $form['wrapper']['you_did_it'] inside the conditional 'if' statement is never reached. Therefore it will not be part of the first run of the form. when you click submit after the form has loaded, $form_state['values'] will now not be empty, and in particular, $form_state['values']['yes_box'] has a value. If you entered a value of 'yes' before hitting submit, the conditional evaluates to true, and now $form['wrapper']['you did it'] is included in the form. Since you told your #ahah in the submit button to refresh everything inside of $form['wrapper'], and $form['wrapper']['you_did_it'] is part of that, it will now be returned to the form, being added dynamically through AHAH.
This is easier if you understand multi-step forms in Drupal. AHAH form are essentially exactly the same as a multi-step form, they just perform the step without a page load instead of requiring one. I really recommend reading and doing this tutorial. It will give you a solid understanding of the form api, and in particular, it explains how to do multi-step forms when you get towards the bottom. Once you have figured out how they work, you can see how my example above ties in with the idea of multi-step forms. Good luck!
Contact me to contract me for D7 -> D10/11 migrations.
Thanks, If anybody could help
Thanks, If anybody could help me out with the following issue, I will be forever grateful.
I got it working for the most part, but when there is a problem when after some AHAH work is done, and a form is submitted with errors it prints out an AHAH string.
Here is the error
{ "status": true, "data": "\x3cdiv class=\"messages error\"\x3e\n \x3cul\x3e\n \x3cli\x3eName field is required.\x3c/li\x3e\n \x3cli\x3ePhone field is required.\x3c/li\x3e\n \x3cli\x3eEmail field is required.\x3c/li\x3e\n \x3c/ul\x3e\n\x3c/div\x3e\n\x3cdiv class=\"form-item\" id=\"edit-text-choice-wrapper\"\x3e\n \x3clabel for=\"edit-text-choice\"\x3eText Choice: \x3c/label\x3e\n \x3cselect name=\"text_choice\" class=\"form-select\" id=\"edit-text-choice\" \x3e\x3coption value=\"0\"\x3ePlease choose a text option\x3c/option\x3e\x3coption value=\"1\"\x3eModern Hebrew / English\x3c/option\x3e\x3coption value=\"2\"\x3eConservative (with Lieberman) / English\x3c/option\x3e\x3coption value=\"3\"\x3eAramaic / English\x3c/option\x3e\x3coption value=\"4\"\x3eCommitment Vows\x3c/option\x3e\x3coption value=\"5\"\x3eContemporary English\x3c/option\x3e\x3coption value=\"6\"\x3eTraditional English\x3c/option\x3e\x3coption value=\"7\"\x3eAnniversary English\x3c/option\x3e\x3coption value=\"8\"\x3eModern Hebrew and Modern English (Ketubah)\x3c/option\x3e\x3coption value=\"9\"\x3eBlank\x3c/option\x3e\x3c/select\x3e\n\x3c/div\x3e\n", "settings": { "ahah": { "edit-print-name": { "url": "/~jbrown/20ci/?q=ahah_helper/text_choice", "event": "change", "keypress": null, "wrapper": "text-choice-wrapper", "selector": "#edit-print-name", "effect": "none", "method": "replace", "progress": { "type": "throbber" }, "button": false } } } }
-----------------
Use case, miss entering name, but filled out everything else. Click submit, it shows the name is missing. Fix error or leave blank still and click submit then get the above error message.
The following is the form code, it blows up when changing the text choice field that and the form has a validation error (missing name, etc).
function orderForm($form_state)
{
$form = array();
// Register the form with ahah_helper so we can use it. Also updates
// $form_state['storage'] to ensure it contains the latest values that have
// been entered, even when the form item has temporarily been removed from
// the form. So if a form item *once* had a value, you *always* can retrieve
// it.
ahah_helper_register($form, $form_state);
global $base_path;
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => '',
'#size' => 60,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone'),
'#size' => 12,
'#maxlength' => 15,
'#required' => TRUE,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Email'),
'#size' => 60,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['wedding_date'] = array(
'#type' => 'date',
'#title' => 'Wedding Date',
'#required' => TRUE,
);
$form['address'] = array(
'#type' => 'textfield',
'#title' => t('Address'),
'#size' => 60,
'#maxlength' => 100,
);
$form['city'] = array(
'#type' => 'textfield',
'#title' => t('City'),
'#size' => 60,
'#maxlength' => 100,
);
$form['state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#default_value' => 'Please select a state',
'#options' => array( 'Please select a state' => 'Please select a state', 'sAL'=>"Alabama",'AK'=>"Alaska",'AZ'=>"Arizona",'AR'=>"Arkansas",'CA'=>"California",'CO'=>"Colorado",'CT'=>"Connecticut",'DE'=>"Delaware",'DC'=>"District Of Columbia",'FL'=>"Florida",'GA'=>"Georgia",'HI'=>"Hawaii",'ID'=>"Idaho",'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas",'KY'=>"Kentucky",'LA'=>"Louisiana",'ME'=>"Maine",'MD'=>"Maryland", 'MA'=>"Massachusetts",'MI'=>"Michigan",'MN'=>"Minnesota",'MS'=>"Mississippi",'MO'=>"Missouri",'MT'=>"Montana",'NE'=>"Nebraska",'NV'=>"Nevada",'NH'=>"New Hampshire",'NJ'=>"New Jersey",'NM'=>"New Mexico",'NY'=>"New York",'NC'=>"North Carolina",'ND'=>"North Dakota",'OH'=>"Ohio",'OK'=>"Oklahoma", 'OR'=>"Oregon",'PA'=>"Pennsylvania",'RI'=>"Rhode Island",'SC'=>"South Carolina",'SD'=>"South Dakota",'TN'=>"Tennessee",'TX'=>"Texas",'UT'=>"Utah",'VT'=>"Vermont",'VA'=>"Virginia",'WA'=>"Washington",'WV'=>"West Virginia",'WI'=>"Wisconsin",'WY'=>"Wyoming"),
);
$form['zip'] = array(
'#type' => 'textfield',
'#title' => t('Zip'),
'#size' => 5,
'#maxlength' => 5,
);
$val = 'Please select an illumination';
if( isset( $form_state['storage']['print_name'] ) )
$val = $form_state['storage']['print_name'];
$form['print_name'] = array(
'#type' => 'select',
'#title' => t('Print Name' ),
'#options' => getIlluminations(),
'#suffix' => '
'#ahah' => array(
'event' => 'change',
// This is the "magical path". Note that the parameter is an array of
// the parents of the form item of the wrapper div!
'path' => ahah_helper_path(array('text_choice')),
'wrapper' => 'text-choice-wrapper',
),
);
if( isset( $form_state['storage']['print_name'] ) )
{
$idx = $form_state['storage']['print_name'];
$illuminations = getIlluminations();
$print_listings = getPrintings($illuminations[$idx]);
if( count( $print_listings ) )
{
$new_list = array();
$new_list[] = 'Please choose a text option';
foreach($print_listings as $item)
$new_list[] = $item;
$form['text_choice'] = array(
'#type' => 'select',
'#title' => t('Text Choice' ),
'#default_value' => 'Please choose a text option',
'#options' => $new_list,
);
}
else
{
$form['text_choice'] = array(
'#type' => 'textfield',
'#title' => t('Text Choice' ),
'#default_value' => 'No Options',
'#disabled' => TRUE,
);
}
}
$form['personalization'] = array(
'#type' => 'checkbox',
'#title' => t( 'Personalization' ),
'#suffix' => 'If checked Click here for a personalization page to send in separately.',
'#prefix' => '
',
);
$form['custom_text'] = array(
'#type' => 'checkbox',
'#title' => t( 'I have my own custom text' ),
);
$form['optional_signature'] = array(
'#type' => 'checkbox',
'#title' => t( 'Optional archival signature pen: $5' ),
);
$form['payment'] = array(
'#type' => 'radios',
'#title' => t('I am planning to pay with'),
'#default_value' => t('Unsure'),
'#options' => array(t('Check/Money Order'), t('PayPal'), t('Unsure')),
);
$form['special_instructions'] = array(
'#type' => 'textarea',
'#title' => t('Special Instructions'),
'#default_value' => '',
'#required' => FALSE
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
return $form;
}