0

I have ConfigFormBase. It has a form element with '#type' => 'entity_autocomplete'.

When I add my custom element validation by '#element_validate' => [[get_class($this), 'validateReferencedItemExistence']] the original element validation which comes by the element type (Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete) won't be added anymore.

After spending hours in the jungle of the FormBuilder class code I finally found the corresponding lines:

   if (isset($element['#type']) && empty($element['#defaults_loaded']) && ($info = $this->elementInfo->getInfo($element['#type']))) {
      // Overlay $info onto $element, retaining preexisting keys in $element.
      $element += $info;
      $element['#defaults_loaded'] = TRUE;
    }

I find this kinda bad approach, that you cannot extend the list of the validation items. What is the reason behind it?

How could I make a workaround / solve this issue?

(Just by calling Drupal\Core\Entity\Element\EntityAutocomplete::validateEntityAutocomplete in my custom validation I would create an unnecessary depedency in my code. (Just think about if they change their way of validation with a different function))

  • You need to use the syntax PHP has for adding items to an array. If you use $array['item'] = []; you are replacing $array['item']; if you use $array['item'][] = []; you are adding items (supposing that $array['item'] is an array). – kiamlaluno Apr 11 '17 at 12:35
  • Am aware of that, however this is not the situation here, because if the key already exists, the element type validation won't be added anymore by the FormBuilder class. – ssibal Apr 11 '17 at 20:57
1

You need add the definitions defined in the getInfo() method of that type explicitly yourself, there is currently no other way to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.