Issue #2894634 by amateescu: Allow entity-level validations to flag errors on form elements
parent
2db1774546
commit
5ea0e3ae8d
|
@ -235,7 +235,7 @@ class ContentEntityForm extends EntityForm implements ContentEntityFormInterface
|
||||||
// Flag entity level violations.
|
// Flag entity level violations.
|
||||||
foreach ($violations->getEntityViolations() as $violation) {
|
foreach ($violations->getEntityViolations() as $violation) {
|
||||||
/** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
|
/** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
|
||||||
$form_state->setErrorByName('', $violation->getMessage());
|
$form_state->setErrorByName(str_replace('.', '][', $violation->getPropertyPath()), $violation->getMessage());
|
||||||
}
|
}
|
||||||
// Let the form display flag violations of its fields.
|
// Let the form display flag violations of its fields.
|
||||||
$this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
|
$this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations, $form, $form_state);
|
||||||
|
|
|
@ -68,6 +68,11 @@ class EntityConstraintViolationList extends ConstraintViolationList implements E
|
||||||
if ($this->entity->hasField($field_name)) {
|
if ($this->entity->hasField($field_name)) {
|
||||||
$this->violationOffsetsByField[$field_name][$offset] = $offset;
|
$this->violationOffsetsByField[$field_name][$offset] = $offset;
|
||||||
}
|
}
|
||||||
|
// If the first part of the violation property path is not a valid
|
||||||
|
// field name, we're dealing with an entity-level validation.
|
||||||
|
else {
|
||||||
|
$this->entityViolationOffsets[$offset] = $offset;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$this->entityViolationOffsets[$offset] = $offset;
|
$this->entityViolationOffsets[$offset] = $offset;
|
||||||
|
|
|
@ -25,6 +25,11 @@ class EntityTestEntityLevelValidator extends ConstraintValidator {
|
||||||
$this->context->buildViolation($constraint->message)
|
$this->context->buildViolation($constraint->message)
|
||||||
->addViolation();
|
->addViolation();
|
||||||
}
|
}
|
||||||
|
if ($value->name->value === 'entity-level-violation-with-path') {
|
||||||
|
$this->context->buildViolation($constraint->message)
|
||||||
|
->atPath('test.form.element')
|
||||||
|
->addViolation();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,6 +149,10 @@ class FieldWidgetConstraintValidatorTest extends KernelTestBase {
|
||||||
|
|
||||||
$errors = $this->getErrorsForEntity($entity);
|
$errors = $this->getErrorsForEntity($entity);
|
||||||
$this->assertEqual($errors[''], 'Entity level validation');
|
$this->assertEqual($errors[''], 'Entity level validation');
|
||||||
|
|
||||||
|
$entity->name->value = 'entity-level-violation-with-path';
|
||||||
|
$errors = $this->getErrorsForEntity($entity);
|
||||||
|
$this->assertEqual($errors['test][form][element'], 'Entity level validation');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue