Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clean way to access form options in event listener

Tags:

forms

symfony

How can I access $options passed to buildForm() from an event listener in a clean way? Please see below code sample:

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'preSetDataListener']);
    }

    public function preSetDataListener(FormEvent $event)
    {
        // how would I access $options from buildFrom here?
    }
like image 577
Veelkoov Avatar asked Mar 08 '16 09:03

Veelkoov


People also ask

What is eventListener in JavaScript?

An event listener is a procedure in JavaScript that waits for an event to occur. The simple example of an event is a user clicking the mouse or pressing a key on the keyboard.

How do you check if an element has an event listener?

Right-click on the search icon button and choose “inspect” to open the Chrome developer tools. Once the dev tools are open, switch to the “Event Listeners” tab and you will see all the event listeners bound to the element. You can expand any event listener by clicking the right-pointing arrowhead.

What is the role of event listeners in event handling?

Event listeners represent the interfaces responsible to handle events. Java provides various Event listener classes, however, only those which are more frequently used will be discussed. Every method of an event listener method has a single argument as an object which is the subclass of EventObject class.

How do you call a function in addEventListener?

With the addEventListener() method you can specify the propagation type by using the "useCapture" parameter: addEventListener(event, function, useCapture); The default value is false, which will use the bubbling propagation, when the value is set to true, the event uses the capturing propagation.


2 Answers

$event->getForm()->getConfig()->getOptions()
like image 141
Arthur Avatar answered Oct 01 '22 22:10

Arthur


Don't rely on $event->getForm()->getConfig()->getOptions(). It's not meant to be used by us. I've opened an issue on the Symfony bug tracker with this problem and they told me to inherit variables into the anonymous function instead. See this example.

Pay attention to the use keyword.

->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options) {
    // here you can use $options
}

That gets the $options array from the parent scope and injects it into the event listener function. It's a PHP feature.

Oh, and this means you'll have to either directly pass an anonymous function as an argument to addEventListener() (as in the example above) or define it inside buildForm() as a normal variable, like this:

$listener = function (FormEvent $event) use ($options) {
    // do something
}

$builder
    ->addEventListener(FormEvents::PRE_SET_DATA, $listener);
like image 30
crimson_king Avatar answered Oct 01 '22 22:10

crimson_king