function FieldWidgetConstraintValidatorTest::getErrorsForEntity

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::getErrorsForEntity()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::getErrorsForEntity()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php \Drupal\KernelTests\Core\Entity\FieldWidgetConstraintValidatorTest::getErrorsForEntity()

Gets the form errors for a given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity

array $hidden_fields: (optional) A list of hidden fields.

Return value

array The form errors.

2 calls to FieldWidgetConstraintValidatorTest::getErrorsForEntity()
FieldWidgetConstraintValidatorTest::testEntityLevelConstraintValidation in core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php
Tests entity level constraint validation.
FieldWidgetConstraintValidatorTest::testValidationWithCompositeConstraint in core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php
Tests widget constraint validation with composite constraints.

File

core/tests/Drupal/KernelTests/Core/Entity/FieldWidgetConstraintValidatorTest.php, line 81

Class

FieldWidgetConstraintValidatorTest
Tests validation constraints for FieldWidgetConstraintValidatorTest.

Namespace

Drupal\KernelTests\Core\Entity

Code

protected function getErrorsForEntity(EntityInterface $entity, $hidden_fields = []) {
    $entity_type_id = 'entity_test_composite_constraint';
    $display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type_id, $entity_type_id);
    foreach ($hidden_fields as $hidden_field) {
        $display->removeComponent($hidden_field);
    }
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity, $form, $form_state);
    $form_state->setFormObject(\Drupal::entityTypeManager()->getFormObject($entity_type_id, '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.
    
    /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
    $form_object = $form_state->getFormObject();
    $form_object->setEntity($entity)
        ->setFormDisplay($display, $form_state)
        ->validateForm($form, $form_state);
    return $form_state->getErrors();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.