1

I'm trying to pass some extra parameters to my FormEvent Listener, which typically just takes one argument, the actual event itself. I tried doing something like:

$builder->addEventListener(FormEvents::PRE_SET_DATA,
    function($event, $extraData) {
        //Do stuff
    }
);

However, this returns an error stating that it's missing the second argument for this function. I'm open to any suggestions! Thanks!

Sammaron
  • 196
  • 1
  • 3
  • 14

1 Answers1

9

The event listener closure only has the event passed to it so your $extraData will never get passed to it via the listener.

If you want to use data from elsewhere in the form inside the closure then you need to pass it in the the closure using a use like so..

$extraData = array('some' => 'stuff');

$builder->addEventListener(FormEvents::PRE_SET_DATA,
    function($event) use ($extraData) {
        //Do stuff
    }
);

Edit

Sorry, I copied and pasted your closure last night which included the function($event, $extraData) and then I forgot to remove it. The use statement is all that is needed. The $extraData that is an argument will not be set by the event so will (probably) come out as null, and might even overwrite the one passed in via the use.

Going from the information in this answer (paraphrased) "$extraData is bound when the function is defined and the arguments are bound when the function is called" would lead me to believe that overwrite may probably happen.

Community
  • 1
  • 1
qooplmao
  • 17,622
  • 2
  • 44
  • 69
  • have totally seen this before, but thanks for jogging my memory ;) as a followup question, do you actually need the additional argument in the closure? or is the `use ($extraData)` enough? (about to test, but bonus points if you know) – Sammaron Jun 26 '14 at 21:17
  • 1
    It's not necessary or the same variable even. I'm fairly certain that the first $extraData is null or at least not the array you are expecting. Rename the first one to $foo and var_dump it inside the closure to see. – Jody Jun 27 '14 at 05:38
  • By 'the first one', you mean the one in the function definition? I think you just get an error if you leave the extra argument in there, as when the closure is called, it's not passed this second parameter, and so it gets confused. – Sammaron Jun 27 '14 at 15:07
  • That may be the case as well. Qoop's updated answer is correct. – Jody Jun 27 '14 at 15:17
  • Cheers. Try, try again and all that. – qooplmao Jun 27 '14 at 15:23
  • @Qoop is there any way to pass additional parameters using following declaration ? `$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));` – V-Light Apr 16 '15 at 07:36
  • Not as far as I know. Listeners are called using `call_user_func($listener, $event, $eventName, $this)` (which would equate to `call_user_func(array($this, 'onPreSubmit'), FormEvents::PRE_SUBMIT, $eventName, $dispatcher)`) so, as far as I know, unless you use an anonymous function (like above) the arguments are only going to be the event, name and dispatcher. – qooplmao Apr 16 '15 at 08:07