vendor/symfony/form/Extension/Validator/EventListener/ValidationListener.php line 55

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Validator\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\Extension\Validator\Constraints\Form;
  13. use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
  14. use Symfony\Component\Form\FormEvent;
  15. use Symfony\Component\Form\FormEvents;
  16. use Symfony\Component\Validator\Validator\ValidatorInterface;
  17. /**
  18.  * @author Bernhard Schussek <bschussek@gmail.com>
  19.  */
  20. class ValidationListener implements EventSubscriberInterface
  21. {
  22.     private $validator;
  23.     private $violationMapper;
  24.     /**
  25.      * {@inheritdoc}
  26.      */
  27.     public static function getSubscribedEvents()
  28.     {
  29.         return [FormEvents::POST_SUBMIT => 'validateForm'];
  30.     }
  31.     public function __construct(ValidatorInterface $validatorViolationMapperInterface $violationMapper)
  32.     {
  33.         $this->validator $validator;
  34.         $this->violationMapper $violationMapper;
  35.     }
  36.     /**
  37.      * Validates the form and its domain object.
  38.      *
  39.      * @param FormEvent $event The event object
  40.      */
  41.     public function validateForm(FormEvent $event)
  42.     {
  43.         $form $event->getForm();
  44.         if ($form->isRoot()) {
  45.             // Form groups are validated internally (FormValidator). Here we don't set groups as they are retrieved into the validator.
  46.             foreach ($this->validator->validate($form) as $violation) {
  47.                 // Allow the "invalid" constraint to be put onto
  48.                 // non-synchronized forms
  49.                 // ConstraintViolation::getConstraint() must not expect to provide a constraint as long as Symfony\Component\Validator\ExecutionContext exists (before 3.0)
  50.                 $allowNonSynchronized = (null === $violation->getConstraint() || $violation->getConstraint() instanceof Form) && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
  51.                 $this->violationMapper->mapViolation($violation$form$allowNonSynchronized);
  52.             }
  53.         }
  54.     }
  55. }