Change record status: 
Project: 
Introduced in branch: 
8.0.x
Introduced in version: 
8.0.0-beta11
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:

... more information on https://github.com/symfony/symfony/blob/2.7/UPGRADE-3.0.md#validator
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
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done