0

I have a form that gets values set in the variable table if any have been submitted. All of the fields work as expected except a radios field, which always shows the default. Here's the relevant code

$one = variable_get('one', array());

$form['one']['visibility'] = array(
 '#type' => 'radios',
 '#title' => t('The title'),
 '#options' => array(0 => t('Yes'), 1 => t('No')),
 '#default_value' => (!empty($one['visibility']))
 ? $one['visibility']
 : 1,
);

I had thought that keying the options array with strings would solve this but it doesn't. I'm still Googling, but I'm wondering if there's something easy I'm missing.

2 Answers 2

1

You'll have to refer to the Form API docs for radios for tips.

Second, it might make things easier to reason about if you do:

$one = variable_get('one', array('visibility' => 0));
$is_visible = $one['visibility'];

$form['one']['visibility'] = array(
 '#type' => 'radios',
 '#title' => t('The title'),
 '#options' => array(0 => t('Yes'), 1 => t('No')),
 '#default_value' => $is_visible,
);
0

I have another field of radios in the form that works correctly, the difference is that the keys are strings, so I tried this again in the above code, this time using strings like "yes" and "no" for keys as well as values and it worked. For some reason, using 1 and 2, or "1" and "2" for array keys will not work as expected in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.