Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By dawehner on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.0.x
Introduced in version:
8.0.0-beta11
Issue links:
Description:
The symfony validator integration got updated to be compatible with symfony 2.5 and above.
This results in new typehints for code directly dealing with validating custom objects:
Before | After |
---|---|
Symfony\Component\Validator\ValidatorInterface |
Symfony\Component\Validator\Validator\ValidatorInterface |
\Symfony\Component\Validator\ExecutionContextInterface |
\Symfony\Component\Validator\Context\ExecutionContextInterface |
While doing that a couple of changes for ConstraintValidators have been introduced:
- Its easier now to access the typed data object in a validator for a specific property:
Before
class MyCustomConstraintValidator ... public function validate($value, Constraint $constraint) { $typed_data = $this->context->getMetadata()->getTypedData(); } }
After
use Drupal\Core\TypedData\Validation\TypedDataAwareValidatorTrait; class MyCustomConstraintValidator ... use TypedDataAwareValidatorTrait; // A new trait got introduced. public function validate($value, Constraint $constraint) { $typed_data = $this->getTypedData(); } }
- Empty field item lists are now passed as empty item list, not as NULL:
Before
public function validate($items, Constraint $constraint) { if (!isset($items)) { return; } }
After
public function validate($items, Constraint $constraint) { $item = $items->first(); if (!isset($item)) { return; } }
- For adding violations with another argument besides the message and its parameter the new
ViolationBuilder
has to be used:
Before
$this->context->addViolation('Please enter a number great than %min.', array('%min' => 5), $invalid_value);
After
$this->context->buildViolation('Please enter a number great than %min.', array('%min' => 5)) ->setInvalidValue($invalid_value) ->addViolation();
Impacts:
Module developers