Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For anyone else looking for help changing their validators to event subscribers (as it is slightly different to normal subscribers) follow this:</p> <h2>Step 1</h2> <p>Change:</p> <p><code>$builder-&gt;addValidator(new AddNameFieldValidator());</code><br> to<br> <code>$builder-&gt;addEventSubscriber(new AddNameFieldSubscriber());</code></p> <h2>Step 2</h2> <p>Replace your validator class (and all the namespaces) to a subscriber class. Your subscriber class should look like the following: </p> <pre><code>// src/Acme/DemoBundle/Form/EventListener/AddNameFieldSubscriber.php namespace Acme\DemoBundle\Form\EventListener; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\Form\FormError; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class AddNameFieldSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return array(FormEvents::POST_BIND =&gt; 'postBind'); } public function postBind(FormEvent $event) { $data = $event-&gt;getData(); $form = $event-&gt;getForm(); $form-&gt;addError(new FormError('oh poop')) } } </code></pre> <p><strong>You do not need to register the subscriber in a service file (yml or otherwise)</strong></p> <hr> <p>Reference: <a href="http://symfony.com/doc/2.2/cookbook/form/dynamic_form_modification.html#adding-an-event-subscriber-to-a-form-class" rel="nofollow">http://symfony.com/doc/2.2/cookbook/form/dynamic_form_modification.html#adding-an-event-subscriber-to-a-form-class</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload