I'm trying to use "#states". When my checkbox is true, I want to make "required" another field. Drupal prints the "required" asterisk but not correctly.
Here it's the code:
// Return origin.
$form['search']['return']['return_ori'] = [
'#attributes' => [
'class' => ['field-autocomplete', 'ret-ori-name'],
'data-type' => 'busstop',
'placeholder' => t('Write your origin', [], ['context' => 'Backend']),
'required' => TRUE,
],
'#default_value' => !empty($_GET['return_ori']) ? $_GET['return_ori'] : '',
'#prefix' => '<div class="row"><div class="col-md-5">',
'#states' => [
'required' => [
':input[name="search_return"]' => [
'checked' => TRUE,
],
],
],
'#size' => 40,
'#suffix' => '</div>',
'#title' => t('Return origin'),
'#type' => 'textfield',
];
// Return trip.
$form['search']['search_return'] = [
'#default_value' => !empty($_GET['search_return']) ? $_GET['search_return'] : FALSE,
'#prefix' => '<div class="row"><div class="col-md-5">',
'#suffix' => '</div></div>',
'#title' => t('Search return trip'),
'#type' => 'checkbox',
'#attributes' => [
'class' => ['search-return-trip'],
],
];
The submit button has not specific method, so Drupal should handle it. When I press it, the form is sended ignoring my new required field.
Thanks all for help!!
#states
is client side, not server side. You can't use it to validate a required field.