$form =
[];
$form_state =
new FormState();
$display->
buildForm($entity,
$form,
$form_state);
// Pretend the form has been built.
$form_state->
setFormObject(\Drupal::
entityTypeManager()->
getFormObject($entity_type, 'default'
));
\Drupal::
formBuilder()->
prepareForm('field_test_entity_form',
$form,
$form_state);
\Drupal::
formBuilder()->
processForm('field_test_entity_form',
$form,
$form_state);
// Validate the field constraint.
$form_state->
getFormObject()->
setEntity($entity)->
setFormDisplay($display,
$form_state);
$entity =
$form_state->
getFormObject()->
buildEntity($form,
$form_state);
$display->
validateFormValues($entity,
$form,
$form_state);
$errors =
$form_state->
getErrors();
$this->
assertEquals('Widget constraint has failed.',
$errors['name'
], 'Constraint violation at the field items list level is generated correctly'
);
$this->
assertEquals('Widget constraint has failed.',
$errors['test_field'
], 'Constraint violation at the field items list level is generated correctly for an advanced widget'
);
} /**
* Gets the form errors for a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity
* @param array $hidden_fields
* (optional) A list of hidden fields.
*
* @return array
* The form errors.
*/