Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By bkai on
So I have a conditional form field and need it to be required only if a specific checkbox is selected from a previous form field. Setting it to required with only the conditional "#states" option keeps it required even if the condition is to not display this field. So I thought perhaps I could add a condition to the value of "#required" but it doesn't appear to be working. Am I out of luck with this one?
Here's an example:
$form['field2'] = array(
'#type' => 'radios',
'#title' => t('some question?'),
'#options' => array(
'yes' => t('Yes'),
'no' => t('No')
),
'#required' => ($form_state['values']['field1']['checkbox3'] == 0 ? FALSE : TRUE), //###<--- this is what I'm trying to figure out ###
'#states' => array(
'visible' => FALSE,
'visible' => array(
':input[name="field1[checkbox3]"]' => array(
'checked' => TRUE
),
)
)
);
Note that field1 will contain multiple checkboxes.