How to make an entity reference field required using states I need to make an entity reference field in my form be required based on the state, the code below is working with other fields, but not my entity reference field. 

$form['schedule_publish_date'] ['#states'] =  array(
 'required' =&gt; array(
  ':input[name="moderation_state[0]"]' =&gt; array('value' =&gt;"published"),
 ),
);


this is the source code of the entity reference field. Am I putting in the wrong name for the form item? 

&lt;fieldset data-drupal-selector="edit-schedule-publish-date" id="edit-schedule-publish-date" class="js-form-item form-item js-form-wrapper form-wrapper"&gt;
  &lt;legend&gt;
&lt;span class="fieldset-legend"&gt;Schedule Publish Date&lt;/span&gt;



  
            


      &lt;/div&gt;




the moderation state is not what I am having trouble reaching. It's the schedule publish date, which is an entity reference and doesnt become "required" when it should.
There isn't a specific form element with the moderation state, since they are added as action submit buttons, but you could add one like this:

  if (!empty($form['moderation_state']['widget'])) {
    $form['moderation_state_hidden'] = [
      '#type' =&gt; 'hidden',
      '#title' =&gt; 'moderation_state_hidden',
      '#value' =&gt; $form['moderation']['widget'][0]['#default_value'],
    ];
  }


Then you could refer to it in your states array like this:

$form['schedule_publish_date'] ['#states'] =  array(
 'required' =&gt; array(
  ':input[name="moderation_state_hidden"]' =&gt; array('value' =&gt;"published"),
 ),
);


But using states doesn't really make sense because you can't change the moderation status of a hidden field or a non-ajax submit button.

So in that case, you could just update the form element to be required

  if (!empty($form['moderation_state']['widget']) &amp;&amp; $form['moderation_state']['widget'][0]['#default_value'] == 'published') {
    $form['schedule_publish_date']['widget'][0]['target_id']['#required'] = TRUE;
  }