How to make my validateForm working? In a custom module of mine, I have two custom entities A and B with a one to many relationship from A to B.
This is Edit form for EntityB:


I want to have this check in validateForm: if 'Contact' (boolean) is checked, 'Adhérent' (entity_reference to EntityA) shouldn't be empty.  

Here is my validateForm:  

  public function validateForm(array &amp;$form, FormStateInterface $form_state) {
    parent::validateForm($form, $form_state);
    $values=$form_state-&gt;getValues();
ksm($values);
    if ($values['estlecontact']['value']==1 &amp;&amp; $values['adherentid']['target_id']==NULL) {
      $form_state-&gt;setErrorByName('estlecontact', t('L\'Adhérent est obligatoire quand la Personne est désignée comme Contact.'));
    }
  }


In this case, I should not have the error message and still...
What am I doing wrong?
Here is the result of ksm:
I figured it out!
this works:  

$values=$form_state-&gt;getValues();  
if ($values['estlecontact']['value']==1 &amp;&amp; $values['adherentid'][0]['target_id']==NULL) {  
  $form_state-&gt;setErrorByName('estlecontact', t('L\'Adhérent est obligatoire quand la Personne est désignée comme Contact.'));  
}


Note that $values['adherentid'] is a mutliple value field. I was missing an array level. Adding [0] before accesing target_id solves the problem.