#States required not working 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'    =&gt; [
      'class'       =&gt; ['field-autocomplete', 'ret-ori-name'],
      'data-type'   =&gt; 'busstop',
      'placeholder' =&gt; t('Write your origin', [], ['context' =&gt; 'Backend']),
      'required'    =&gt; TRUE,
    ],
    '#default_value' =&gt; !empty($_GET['return_ori']) ? $_GET['return_ori'] : '',
    '#prefix'        =&gt; '&lt;div class="row"&gt;&lt;div class="col-md-5"&gt;',
    '#states'        =&gt; [
      'required' =&gt; [
        ':input[name="search_return"]' =&gt; [
          'checked' =&gt; TRUE,
        ],
      ],
    ],
    '#size'          =&gt; 40,
    '#suffix'        =&gt; '&lt;/div&gt;',
    '#title'         =&gt; t('Return origin'),
    '#type'          =&gt; 'textfield',
  ];
// Return trip.
  $form['search']['search_return'] = [
    '#default_value' =&gt; !empty($_GET['search_return']) ? $_GET['search_return'] : FALSE,
    '#prefix'        =&gt; '&lt;div class="row"&gt;&lt;div class="col-md-5"&gt;',
    '#suffix'        =&gt; '&lt;/div&gt;&lt;/div&gt;',
    '#title'         =&gt; t('Search return trip'),
    '#type'          =&gt; 'checkbox',
    '#attributes'    =&gt; [
      'class' =&gt; ['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!!
Check the "name" attribute of your search return checkbox by inspecting it within your browser. You may find the name="" isn't actually name="search_return". It may instead be name="search[search_return]" or something similar. So you need to update your states code to whatever the name is