I'm making a module where the content type and fields are created by install. Now i want to make one of those fields only to show up when an option is selected. I did this with AJAX, in some way I can't make it work with the field as defined in the module.install file. I have a radio button selector for two options. When selecting the first option there should show up a selectbox with a list of nodes to select. When selecting the second option two textfields should be shown instead. When I try to add content of this type I get the error message (when changing the value from the radio buttons):
An illegal choice has been detected. Please contact the site administrator.
Can someone give me some advice to make this work? Thanks in advance!
----
Here is my code from the install file:
function _concert_installed_fields() {
$t = get_t();
return array(
// other fields left out
'venue_node' => array(
'field_name' => 'venue_node',
'label' => 'Select a venue from the list',
'type' => 'list_integer',
'cardinality' => '1',
'foreign keys' => array(),
'indexes' => array(
'value' => array(
0 => 'value',
),
),
'module' => 'list',
'settings' => array(
'allowed_values_function' => '_concert_content_type_venue_options_list',
),
),
'venue_radio' => array(
'field_name' => 'venue_radio',
'cardinality' => 1,
'type' => 'list_boolean',
)
);
From my .module file:
function concert_form_alter(&$form, &$form_state, $form_id) {
if($form_id == "concert_node_form") {
$form['venue_radio'] = array(
'#title' => t("Choose a venue from the database or add one manually"),
'#type' => 'radios',
'#options' => array(0 => t('Add a venue from the database.'),
1 => t('Add a venue manually.'),),
'#ajax' => array(
'callback' => 'concert_venue_callback',
'wrapper' => 'venue_select',
'effect' => 'fade',
),
);
$form['venue_select'] = array(
'#prefix' => '<div id = "venue_select">',
'#suffix' => '</div>',
'#type' => 'fieldset',
);
if (!empty($form_state['values']['venue_radio']) && $form_state['values']['venue_radio'] == "1") {
$form['venue_node']['#type'] = 'hidden';
$form['venue_select']['location'] = array(
'#type' => 'textfield',
'#title' => t('location'),
);
$form['venue_select']['venue_city'] = array(
'#type' => 'textfield',
'#title'=> t('city'),
);
}
else {
$form['venue_select']['venue_node'] = array(
'#type' => 'select',
'#options' => _concert_content_type_venue_options_list(),
'#title' => t('venue node select'),
'#default_value' => '466', // node id is select value
);
if (!empty($form_state['values']['venue_node']))
$form['concert_venue_node']['und']['#default_value'] = $form_state['values']['venue_node'];
}
function concert_venue_callback($form, $form_state) {
return $form['venue_select'];
}
The options from the selectbox are populated by a function in a file accessable for both the .install and the .module file:
function _concert_content_type_venue_options_list() {
$results = db_select('node', 'n')
->fields('n', array('nid', 'title'))
->condition('n.status', 1)
->condition('n.type', 'venue')
->execute();
$options = array();
foreach ($results as $node) {
$options[$node->nid] = $node->title;
}
return $options;
}
Comments
I had same problem - ajax
I had same problem - ajax form, radios and select involved, illegal choice msg... I think the problem is in here: http://drupal.org/node/811542
This is what worked for me:
- I changed radios attribute required to false, and did validation in own validation callback (not your case as you do not have required radios)
- I added empty option to selects
Than it worked.
Setting a default value for
Setting a default value for the radios should solve this problem.
Contact me to contract me for D7 -> D10/11 migrations.
saving the value
Thanks, that solved it! I was so focused on the selectbox I overlooked the radio buttons.
Just one little question. The only problem I have now is that the value is not saved.
I found out about the AJAX altering node form example, where a parallel database is created.
Is this the only possible way to make this work?
Thanks again!
I changed my code like the
I changed my code like the way it was done in the AJAX altering node form example. Everything is fine now.
thank you jaypan
setting the default value to array() solved the problem for me, thank you!!