custom/plugins/LoyxxSeminar/src/Subscriber/ValidationEventSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. namespace LoyxxSeminar\Subscriber;
  3. use Shopware\Core\Framework\Validation\BuildValidationEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Validator\Constraints\All;
  6. use Symfony\Component\Validator\Constraints\Collection;
  7. use Symfony\Component\Validator\Constraints\Email;
  8. use Symfony\Component\Validator\Constraints\Length;
  9. use Symfony\Component\Validator\Constraints\NotBlank;
  10. use Symfony\Component\Validator\Constraints\Required;
  11. class ValidationEventSubscriber implements EventSubscriberInterface
  12. {
  13.     const CAN_VALIDATE false;
  14.     public static function getSubscribedEvents()
  15.     {
  16.         return [
  17.             'framework.validation.order.create' => 'onValidationEventBuild',
  18.             'framework.validation.order.update' => 'onValidationEventBuild',
  19.         ];
  20.     }
  21.     public function onValidationEventBuild(BuildValidationEvent $event)
  22.     {
  23.         if (static::CAN_VALIDATE){
  24.             $definition $event->getDefinition();
  25.             $definition->add(
  26.                 'participants',
  27.                 new All(
  28.                     [
  29.                         'constraints' => new Collection(
  30.                             [
  31.                                 'fields' => [
  32.                                     'first_name' => new Required(
  33.                                         [
  34.                                             new NotBlank(),
  35.                                             new Length(['min' => 3]),
  36.                                         ]
  37.                                     ),
  38.                                     'last_name' => new Required(
  39.                                         [
  40.                                             new NotBlank(),
  41.                                             new Length(['min' => 1]),
  42.                                         ]
  43.                                     ),
  44.                                     'company' => new Required(
  45.                                         [
  46.                                             new NotBlank(),
  47.                                             new Length(['min' => 1]),
  48.                                         ]
  49.                                     ),
  50.                                     'email' => new Required(
  51.                                         [
  52.                                             new NotBlank(),
  53.                                             new Email(),
  54.                                         ]
  55.                                     ),
  56.                                 ],
  57.                             ]
  58.                         ),
  59.                     ]
  60.                 )
  61.             );
  62.         }
  63.     }
  64. }