Dynamic select list in the form (dependent dropdown)
I'm using Drupal seven. I want to make the options in a select list be dependent on the value chosen in another select list in a form. I'm sure this has been asked many times before, but I am having difficulty finding a clear answer for how to do this.
The form is for users to enter a work history. They need to select a squadron which is a node reference to the squadron field type, and this is in a drop-down list. However, the squadron, is dependent upon a city drop-down list. Users first need to select a city which will then filter the options for the squadron. In the squadron content type, I created a taxonomy for city which gets tagged to the squadron.
I would be very grateful for any pointers as to the best way (simplest?) to go about this, or for any useful resources online which would help.
Answers 5
You can use Ajax to accomplish this. Drupal 7 has good Ajax support now. On your first select list (city) you'll need to add Ajax information. Then, the second select list can be populated based on the information in the first. You can also even hide the second select list until an option in the first is selected, and I'll explain how to do that in a bit. First, to set up the basic form:
This is just the basic setup of the elements. Now you'll need a way to determine what options should go in squadron. First you need to make your Ajax callback identified in 'city' select list. In most cases you can just return the element that wraps the ajax element, in this case $form.
Now, when the 'city' select list changes it will rebuild the squadron-wrapper part of the form. Your 'city' value will now be in $form_state['values']. So, when the form is rebuilt we need to determine what options to give to the select list based on the value of 'city'.
Many thanks to jordojuice above. With his help I managed to find a solution. I also refered to the example at http://public-action.org/content/drupal-7-form-api-dependent-lists-and-ajax-form-submission. I eventually used the code below which worked in a custom module. For some reason I couldn't find any of my values in the $form_state values, but was able to find them in $form. Finally, when I tested, I was getting an error message that Drupal had detected an illegal choice in the drop-down. I got round this by commenting out line 1290 in form.inc:
form_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
The final code I used was:
put the line of code i.e
$nodes[''] = '- None -';
afterin ur
sappers_squadron_squadrons function
and that will solve your errorform_error($elements, $t('An illegal choice has been detected. Please contact the site administrator.'));
The root cause of "An illegal choice has been detected. Please contact the site administrator." is that the empty string with the value of 0 added by
$nodes[]="";
is invalid for the field_squadron field.See Advance PHP Programming and Development, but keep in mind that the DANGEROUS_SKIP_CHECK and validated flags are deprecated in D7.
After I removed that line, the error was gone.
Use the Reference field option limit module