Search code examples
drupaldrupal-7drupal-modulesdrupal-fapi

Drupal 7 : Getting the values from radio buttons form api


So this is my form :

$active = array(0 => t('Poster'), 1 => t('Postcard'), 2=>t('Post it'));

$form['radioimage']['active'] = array(
'#type' => 'radios',
'#default_value' => isset($node->active) ? $node->active : 1,
'#options' => $active,
);    

I want to know which radio button was selected. I am trying to access the data but I don't know what its called I can't even use devel for some reason.

I tried below but they all failed

$form_state['values']['radioimage']['active'][0]
$form_state['values']['radioimage']['active']

Solution

  • Drupal flattens the values in the $form_state array by default so

    $form['radioimage']['active']
    

    will actually come out in

    $form_state['values']['active']
    

    If you want to explicitly keep your naming hierarchy then you should set the #tree key on the parent element:

    $form['radioimage'] = array(
      '#type' => 'container',
      '#tree' => TRUE
    );
    

    In that case the value will be in

    $form_state['values']['radioimage']['active']