By TravisJohnston on
I am using entity registration and I have a registration form where i want the user to select what district they are from and then based on their select, prepopulate a list of schools available in the following select list.
Goal:
1) Select the District you belong to
2) Then based on the District you chose, choose a School available in that District
Code I am using but its not working. The numbers being used in the case are the value keys of the districts.
function ces_custom_form_alter(&$form,$form_state,$form_id) {
// if the list field exists for this form, alter it
if($form_id == 'registration-form'){
$district = $form_state['values']['field_member_districts'];
$school = '';
switch ($district) {
case "4450000":
$school = array(
'none'=>'None',
'school1'=>'Deerfield Elementary',
'school2'=>'Hatfield Public School'
);
break;
case "Abington":
$school = array(
'none'=>'None',
'school1'=>'Rowe Elementary',
'school2'=>'Shelburne Falls',
'school3'=>'Buckland School',
'school4'=>'Greenfield School'
);
break;
return $school;
}
$form['field_member_schools'] = array(
'#type' => 'select',
'#title ' => t('School'),
'#options' => $school,
);
}
}
I've also tried it this way:
function ces_custom_get_list_options() {
$field = field_info_field('field_member_districts');
$allowed_values = list_allowed_values($field);
switch ($allowed_values) {
case "4450000":
return array(
'none'=>'None',
'john'=>'John',
'paul'=>'Paul',
'george'=>'George',
'ringo'=>'Ringo'
);
break;
case "Abington":
return array(
'none'=>'None',
'school1'=>'Rowe Elementary',
'school2'=>'Shelburne Falls',
'school3'=>'Buckland School',
'school4'=>'Greenfield School'
);
break;
}
}
// this is hook_form_alter
function ces_custom_form_alter(&$form,$form_state,$form_id) {
// if the list field exists for this form, alter it
if (!empty($form['field_member_schools'])) {
// set the allowed options for the list field
$options = ces_custom_get_list_options(); // returns array of key/value pairs
$form['field_member_schools']['und']['#options'] = $options;
// if the saved node contains saved list options, select/check them
if (!empty($form['#node']->field_member_schools['und'])) {
$saved = $form['#node']->field_member_schools['und'];
foreach($saved as $delta=>$option) {
$form['field_member_schools']['und']['#default_value'][$delta] = $option['value']; // select/check
}
}
}
}
Still no luck. Any thoughts? I have seen some examples but most show creating the entire form and it's fields in the module, but in my case the form is already created and so are the two fields I am using.
Comments
I would suggest using a
I would suggest using a hierarchical vocabulary, a term reference field and the hierarchical select module.
Too much data
That would be great if it was only a few, but there are 504 districts and 1867 schools in total. There is anywhere around 1-10 schools in each district. I'm afraid of the potential impact on the database with that many and also the read out in views so I can report out what they chose.
Thank you kindly,
Travis Johnston
Creo Coding - Let's Start Our Journey!
Owner and Web Developer
http://www.creocoding.com
Facebook - Facebook.com/CreoCoding
LinkedIn - http://lnkd.in/rQrdS7
I will look
I will look into it though, perhaps I'm over thinking it.
Thank you kindly,
Travis Johnston
Creo Coding - Let's Start Our Journey!
Owner and Web Developer
http://www.creocoding.com
Facebook - Facebook.com/CreoCoding
LinkedIn - http://lnkd.in/rQrdS7
After many hours i finally
After many hours i finally found solution. I dynamically created options for multi select field and got error message on submit that choosen select in wrong option.
It requires add new options to allowed_values for that field.
Here is the solution:
In your .module file write in callback function :