I have been trying to implement a basic guest list form that includes a required Email address field along with some optional text fields that are generated using and AHAH submit button. A user can enter some guest names and press an "Add more guests" button which in turn creates a new text field for them to enter a guest name. When they are finished, the can press the "Send" submit button to send their request to a specified email.
Everything works fine with the exception that clicking on the "Add more guests" AHAH submit button performs validation on the whole form so I end up getting an "Email address is required" error if that field has not been filled out when the user presses the "Add more guests" button. I only want the email field to be validated when the whole form is submitted with the "Send" button. It needs to be required before submitting the entire form and I want it to show the required asterisk as well as show an error if the form is submitted without it being filled in.
How can I accomplish this? It's been driving me crazy. I don't believe this problem if you are using AHAH select boxes instead of submit buttons. I am using the AHAH Helper module as well in case that is important to know.
Here is an excerpt of how my form is built:
function guestlist_addguests(&$form_state) {
$form = array();
ahah_helper_register($form, $form_state);
// We check to see if quantity has been set, ie if the form has been submitted yet. If not, we show 1 field, you can change this to however many fields you want to show up as default.
if (!isset($form_state['storage']['quantity'])) {
$quantity = 1;
}
else {
// If the form has been submitted we get the quantity that was stored.
$quantity = $form_state['storage']['quantity'];
// We then do a check to see if the user has clicked the "Add another car" button, if they have we increase the amount by 1.
if (isset($form_state['values']['guests']['add_more']) && $form_state['values']['op'] == 'Add another guest') {
$quantity++;
}
}
// We create a hidden form element to store the amount of fields that the user has added.
$form['quantity'] = array(
'#type' => 'value',
'#value' => $quantity,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('Your E-mail address'),
'#required' => TRUE,
);
$form['guests'] = array(
'#title' => t("Guest List"),
'#prefix' => '<div id="guests-wrapper">',
'#suffix' => '</div>',
'#type' => 'fieldset',
'#description' => t('These are the current guests who will be attending.'),
'#tree' => TRUE,
);
for ($i = 1; $i <= $quantity; $i++) {
// The element name needs to be different for each textfield, otherwise we will only get one value after the form is submitted.
$form['guests']['guest_'. $i] = array(
'#type' => 'textfield',
'#title' => t('Guest name'),
'#size' => 36,
'#default_value' => $form_state['values']['guests']['guest_'. $i],
);
}
// We add in a button with the #ahah element which will handle all our work for us.
$form['guests']['add_more'] = array(
'#type' => 'submit',
'#value' => t('Add another guest'),
'#ahah' => array(
'event' => 'click', // When the button is "clicked", AHAH will do it's job
'path' => ahah_helper_path(array('guests')),
'wrapper' => 'guests-wrapper', // We then define the wrapper which will be changed.
),
'#submit' => array('ahah_helper_generic_submit'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Send'),
);
return $form;
}
So I have 2 submit buttons, one AHAH button to add more guest fields and one regular submit button to send of the data.
Thanks in advance for any advice.
Comments
As an update, I have seen and
As an update, I have seen and done the following but not sure if this is the way to do things right or not:
So basically this checks which button was clicked to cause validation and if it is the AHAH submit button, "Add another guest" for adding additional guest text fields, then it simply clears the error message that would be generated by the empty, required email address field. Then when the "Send" submit button is called, the normal error validation message would be displayed if the email were empty.
Does anyone have a better or more correct method of doing this?
Thanks