I have dependent select list (dynamic dropdown) in my form. When I change a value of the first select list, the second select list is automatically pouplated using ajax callback. Both select fields are required! All works fine, except validation.
There are 3 scenarios when submiting form:
- If I do not change any values of my select lists (leaving default "- select -"), and just click submit button; page refreshes and displays message about required fields - This is working fine!
- If I set values in both lists, click submit, validation passes, form is saved - This is working fine as well!
- If I set value of just one select list and click submit button: BOOM! - All options from that select list are gone.
If I do dsm($form_state['values'])
in validate function I can see that all values are set correctly.
Below is my code:
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'registration_form' && $form['#bundle'] == 'free_registration_volunteer_call') {
$options_first = custom_first_dropdown_options();
$value_field_event_1 = isset($form_state['values']['field_event_1']) ? $form_state['values']['field_event_1']['und'][0]['value'] : key($options_first);
// Setup field_event_1 select and bind ajax
$form['field_event_1']['und']['#options'] = $options_first;
$form['field_event_1']['und']['#default_value'] = $value_field_event_1;
$form['field_event_1']['und']['#ajax'] = array(
'event' => 'change',
'callback' => 'custom_ajax_callback',
'wrapper' => 'field_event_1_role_replace',
);
// Setup field_event_1_role select
$form['field_event_1_role']['und']['#prefix'] = '<div id="field_event_1_role_replace">';
$form['field_event_1_role']['und']['#suffix'] = '</div>';
$form['field_event_1_role']['und']['#options'] = custom_second_dropdown_options($value_field_event_1);
$form['field_event_1_role']['und']['#default_value'] = isset($form_state['values']['field_event_1_role']) ? $form_state['values']['field_event_1_role'] : '';
$form['#validate'][] = 'custom_form_validate';
}
}
/**
* Ajax callback for Event roles dropdown
*/
function custom_ajax_callback($form, $form_state) {
return $form['field_event_1_role'];
}
/**
* Helper function to populate the first dropdown.
*/
function custom_first_dropdown_options() {
$node = node_load(arg(1));
$i = 0;
if(!empty($node->field_events_for_volunteering['und'])) {
foreach ($node->field_events_for_volunteering['und'] as $event) {
if (!$i) $events_list_options['_none'] = '- Select Event -';
$events_list_options[(string)$event['node']->nid] = $event['node']->title;
$i++;
}
return $events_list_options;
}
}
/**
* Helper function to populate the second dropdown.
*/
function custom_second_dropdown_options($key = '') {
if ($key) {
$node = node_load($key);
$i = 0;
if(!empty($node->field_volunteer_roles['und'])) {
foreach ($node->field_volunteer_roles['und'] as $role) {
if (!$i) $role_list_options['_none'] = '- Select Volunteer Role -';
$role_list_options[(string)$role['value']] = $role['value'];
$i++;
}
return $role_list_options;
}
}
return array();
}
/**
* Validate form
*/
function custom_form_validate($form, &$form_state) {
if (empty($form_state['values']['field_event_1']['und'][0]['value'])) {
form_set_error('field_event_1', t('Event 1: field is required'));
}
if (empty($form_state['values']['field_event_1_role']['und'][0]['value'])) {
form_set_error('field_event_1_role', t('Role for event 1: field is required'));
}
if (empty($form_state['values']['field_event_2']['und'][0]['value'])) {
form_set_error('field_event_2', t('Event 2: field is required'));
}
if (empty($form_state['values']['field_event_2_role']['und'][0]['value'])) {
form_set_error('field_event_2_role', t('Role for event 2: field is required'));
}
dsm($form_state['values']);
}
submit handler:
/**
* Custom Form Submit Handler (for free registration)
*/
function custom_form_submit(&$form, &$form_state){
$registration_id = $form_state['build_info']['args'][0]->registration_id;
entity_registration_custom_confirm_registration($registration_id); //Process registration - Set registration state to 'complete'
$node = node_load(arg(1)); // Load current node
$form_state['redirect'] = 'event/registration/success/' . $node->nid; // Redirect to success msg
}
if(!empty($node->field_events_for_volunteering['und'])) {
andif ($key) {
in your first and second select options' callbacks. You must be sure that after submission the select lists are receiving the values from that callbacks. Try to debug that functions also..if
conditions you pointed out - No luck with that.registration_form_*
?arg(1)
is not receiving a nid after the validation fails...