<?php
namespace LoyxxSeminar\Subscriber;
use Shopware\Core\Framework\Validation\BuildValidationEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Required;
class ValidationEventSubscriber implements EventSubscriberInterface
{
const CAN_VALIDATE = false;
public static function getSubscribedEvents()
{
return [
'framework.validation.order.create' => 'onValidationEventBuild',
'framework.validation.order.update' => 'onValidationEventBuild',
];
}
public function onValidationEventBuild(BuildValidationEvent $event)
{
if (static::CAN_VALIDATE){
$definition = $event->getDefinition();
$definition->add(
'participants',
new All(
[
'constraints' => new Collection(
[
'fields' => [
'first_name' => new Required(
[
new NotBlank(),
new Length(['min' => 3]),
]
),
'last_name' => new Required(
[
new NotBlank(),
new Length(['min' => 1]),
]
),
'company' => new Required(
[
new NotBlank(),
new Length(['min' => 1]),
]
),
'email' => new Required(
[
new NotBlank(),
new Email(),
]
),
],
]
),
]
)
);
}
}
}