8000 [Form] Introduce validation events by Seb33300 · Pull Request #47210 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Introduce validation events #47210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Remove static property
  • Loading branch information
Seb33300 committed Aug 27, 2022
commit e23b9d634ecce69824d8240ac7064a85b4ec9350
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ValidationListener implements EventSubscriberInterface
private ViolationMapperInterface $violationMapper;

/** @var FormInterface[][] */
private static array $dispatchEvents = [];
private array $dispatchEvents = [];

public static function getSubscribedEvents(): array
{
Expand All @@ -49,7 +49,7 @@ public function validateForm(FormEvent $event)
// Register events to dispatch during (root form) validation
foreach (ValidatorFormEvents::ALIASES as $eventName) {
if ($form->getConfig()->getEventDispatcher()->hasListeners($eventName)) {
self::$dispatchEvents[$eventName][] = $form;
$this->dispatchEvents[$eventName][] = $form;
}
}

Expand All @@ -71,17 +71,17 @@ public function validateForm(FormEvent $event)

private function dispatchEvents(string $eventName)
{
if (!isset(self::$dispatchEvents[$eventName])) {
if (!isset($this->dispatchEvents[$eventName])) {
return;
}

$event = array_flip(ValidatorFormEvents::ALIASES)[$eventName];

foreach (self::$dispatchEvents[$eventName] as $form) {
foreach ($this->dispatchEvents[$eventName] as $form) {
$event = new $event($form, $form->getData());
$form->getConfig()->getEventDispatcher()->dispatch($event, $eventName);
}

unset(self::$dispatchEvents[$eventName]);
unset($this->dispatchEvents[$eventName]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@
*/
class FormTypeValidatorExtension extends BaseValidatorExtension
{
private ValidatorInterface $validator;
private ViolationMapper $violationMapper;
private bool $legacyErrorMessages;
private ValidationListener $validationListener;

public function __construct(ValidatorInterface $validator, bool $legacyErrorMessages = true, FormRendererInterface $formRenderer = null, TranslatorInterface $translator = null)
{
$this->validator = $validator;
$this->violationMapper = new ViolationMapper($formRenderer, $translator);
$this->validationListener = new ValidationListener($validator, new ViolationMapper($formRenderer, $translator));
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventSubscriber(new ValidationListener($this->validator, $this->violationMapper));
$builder->addEventSubscriber($this->validationListener);
}

public function configureOptions(OptionsResolver $resolver)
Expand Down
0