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

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.     public function validateForm(FormEvent $event)
  37.     {
  38.         $form $event->getForm();
  39.         if ($form->isRoot()) {
  40.             // Form groups are validated internally (FormValidator). Here we don't set groups as they are retrieved into the validator.
  41.             foreach ($this->validator->validate($form) as $violation) {
  42.                 // Allow the "invalid" constraint to be put onto
  43.                 // non-synchronized forms
  44.                 $allowNonSynchronized $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
  45.                 $this->violationMapper->mapViolation($violation$form$allowNonSynchronized);
  46.             }
  47.         }
  48.     }
  49. }