My dynamic Competition form has two basic parts:
1- Basic Info
- Name
- Size Dropdown
2- Ajax Fields
- Options dropdown based on Size
When I change the value in Size, a new combobox is loaded via AJAX with related values.
Here is part of my form:
if ($competition->size == 0)
{
$form['country'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#weight' =>3,
'#required' => FALSE,
'#zurb_class'=> 'cleared',
'#default_value' => $competition->country_id,
'#options' => _ajax_countries_dropdown_options(),
'#prefix' => '<div id="dropdown-competition-size-replace">',
'#suffix' => '</div>',
);
}
else if ($competition->size == 1)
{
$form['continent'] = array(
'#type' => 'select',
'#title' => t('Continent'),
'#default_value' => $competition->continent_id,
'#weight' =>2,
'#required' => FALSE,
'#options' => _ajax_continents_dropdown_options(),
'#prefix' => '<div id="dropdown-competition-size-replace">',
'#suffix' => '</div>',
);
}
else
{
$suffix = '<div id="dropdown-competition-size-replace"></div>';
}
$form['size'] = array(
'#type' => 'select',
'#title' => t('Size'),
'#default_value' => $competition->size,
'#weight' =>1,
'#options' => $options_size,
'#required' => FALSE,
'#ajax' => array(
'callback' => 'ajax_competition_size_dropdown_callback',
'wrapper' => 'dropdown-competition-size-replace',
'method' => 'replace',
'effect' => 'fade',
),
'#suffix' => $suffix,
);
Ajax Callback
function ajax_competition_size_dropdown_callback($form, $form_state) {
$size = $form_state['values']['size'];
if ($size == 0)
{
return array(
'#name' => 'country',
'#type' => 'select',
'#title' => t('Country'),
'#weight' =>3,
'#required' => FALSE,
'#zurb_class'=> 'cleared',
'#default_value' => $competition->country_id,
'#options' => _ajax_countries_dropdown_options(),
'#prefix' => '<div id="dropdown-competition-size-replace">',
'#suffix' => '</div>',
);
}
else if ($size == 1)
{
return array(
'#name' => 'continent',
'#type' => 'select',
'#title' => t('Continent'),
'#default_value' => $competition->continent_id,
'#weight' =>2,
'#required' => FALSE,
'#options' => _ajax_continents_dropdown_options(),
'#prefix' => '<div id="dropdown-competition-size-replace">',
'#suffix' => '</div>',
);
}
else
{
return '<div id="dropdown-competition-size-replace"></div>';
}
}
Steps I made so far:
- When I change the first select when editing, the values are not saved.
- Debugging Backend
- The form changes on the front (AJAX), but it does not change on the backend
- Apparently the problem is I am creating the form fields based on size, Drupal will only recognize this fields. So when I change the select and load the second, it won't parsed by Drupal
- I tried to use $form_state['rebuild'] = TRUE; but it didn't fix anything, actually avoided the redirection after submission.