Here’ the task : I have a drop down list , a simple ‘yes’ or ‘no’ . If yes fill in the the input box.
At first I thought this would need a simple Ajax form call but in fact its even easier than that. We can use the ‘States’ and ‘visible’ values of the Form API [https://api.drupal.org/api/examples/form_example%21form_example_states.inc/function/form_example_states_form/7]
Here’s my code that solved this issue
$form['addtoportfolio'] = array(
'#type' => 'select',
'#prefix' => "Your score was $score%; the pass rate is $pass_rate% ",
'#title' => t('Would you like to add this to your portfolio ?'),
'#required' => TRUE,
'#options' => array(
'n' => t('No'),
'y' => t('Yes'),
),
'#default_value' => t('No'),
);
$form['timetaken'] = array(
'#type' => 'textfield',
'#title' => t('Time Taken ?'),
'#required' => FALSE,
'#description' => "Please enter the time in minutes that it took you to do this quiz.",
'#size' => 4,
'#maxlength' => 10,
'#states' => array(
'visible' => array(
':input[name="addtoportfolio"]' => array('value' => 'y'),
),
),
);
No comments:
Post a Comment