Having a custom validation constraint on an entity reference field using entity browser as form widget, I am trying to get the field light up in the form on validation violation. This is not working for me.

Using the following code in an entity validation constraint:

        ->buildViolation($constraint->message, [])

This works fine for fields that do not use the entity browser widget (the field gets marked red indicating an validation error). But for the entity browser widget the field is not marked in the form, though the validation itself is working correct. It seems to me this is related to the entity browser widget. There is this information about targeting specific properties in complex field widgets, which seems to address this matter. But I cannot seem to find a way to target the right property to make the field light up in the form. Entity Browser widget only have hidden form input elements and targeting does not seem to work on container or div fields as far as I can see, so maybe this is the reason this is not working?

Is there a possibility to have the entity browser widget light up in the form on validation violation?

1 Answer 1


I think this is a limitation of Entity Browser module. Drupal is ultimately using setErrorByName() to add the error to the form and Entity Browser does not provide a name attribute that Drupal can attach it to. So if a name attribute is added somewhere in the entity browser widget Drupal could attach the error. That is, Drupal does only visibly attach the error to (visible) input, select and textareas in the form. Other elements may be targeted and get the class "error" added, but this will not show visibly as an error in the form.

I have created a small module to provide the error flagging for the entity browser widget: https://www.drupal.org/project/entity_browser_validation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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