Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $form_state->cleanValues();
- $this->entity = $this->buildEntity($form, $form_state);
- use SymfonyComponentValidatorConstraints as Assert;
- use SymfonyComponentValidatorContextExecutionContextInterface;
- use SymfonyComponentValidatorMappingClassMetadata;
- use SymfonyComponentValidatorValidation;
- /**
- * Validates my config entity.
- *
- * @return SymfonyComponentValidatorConstraintViolationList
- * List of violations. Empty if valid.
- */
- public function validate() {
- $validator = Validation::createValidator();
- ->addMethodMapping('loadValidatorMetadata')
- ->getValidator();
- return $validator->validate($this);
- }
- /**
- * Defines validation rules for this config entity.
- */
- public static function loadValidatorMetadata(ClassMetadata $metadata) {
- // Same validator rules.
- // Add your own as required.
- // Note this is static, so you don't have $this in scope.
- $metadata->addPropertyConstraint('someProperty', new AssertNotBlank(['message' => 'Some property cannot be blank']));
- // But if you add a validation callback, you can operate in the context
- // of an instance.
- $metadata->addConstraint(new AssertCallback(['methods' => ['someValidationCallback']]));
- }
- /**
- * Validation callback for some field.
- */
- public function someValidationCallback(ExecutionContextInterface $context) {
- if ($this->something === 'something it should not be') {
- $context->buildViolation("Can't let you do that Dave.")
- ->atPath('something')
- ->addViolation();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement