Symfony How to inject parameter in a set method at form submit?

Symfony How to inject parameter in a set method at form submit?,symfony,symfony-forms,Symfony,Symfony Forms,When form is submitted, object's set method (League#setInformation) is called with corresponding data. All is working correctly. (See code below as an example) I need to pass additional parameters to setInformation, namely current user id which is stored in session data. That trick would help keeping session and model separate. Maybe useful in different situations too. Do you know a way to deal with it? class LeagueFormType extends AbstractType { public function buildForm(FormBuilderInt

When form is submitted, object's set method (League#setInformation) is called with corresponding data. All is working correctly. (See code below as an example)

I need to pass additional parameters to setInformation, namely current user id which is stored in session data.

That trick would help keeping session and model separate. Maybe useful in different situations too.

Do you know a way to deal with it?

class LeagueFormType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('name');

        $builder->add('information', 'collection', [
            'type' => new LeagueInformationFormType(),
        ]);
    }

    public function setDefaultOptions(\Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver) {
        $resolver->setDefaults([
            'data_class' => 'xxx\Models\League',
        ]);
    }

    public function getName() {
        return 'league';
    }

}

class League {

    public function getInformation() {
        //...
    }

    public function setInformation($data) {
        ...
    }

}

#1

What I would do is declare form as a service, and inject the data from session. If you can, try to refactor your setInformation() function to two functions for example, so you dont have to provide all information through that one. However I think form events will help you set everything as you like.


#2

If you are using Doctrine2 and the League class is actually a Doctrine2 Entity, I would recommend using a Doctrine2 subscriber/listener.

You can configure the subscriber/listener to do something either just before sending the data to the databse (onFlush), just after telling doctrine about a new entity (persist) or just before updating an existing record (update), whichever is the most appropiate in your case.

Inject the SecurityContext (@security.context in your DIC) into the subscriber/listener to pull out the current user information. (Make sure you check there is a user, because the subscriber wil also be run when nobody is logged in and a League object is saved)

The main advantage of this is that is does not pollute your form or controller. And if for some reason you create a League entity some other way the current user wil also be set.

Some docs: http://doctrine-orm.readthedocs.org/en/latest/reference/events.html

It's a different story if you are not using Doctrine2 though.


#3

Thanks for directions, I'll see what can I do with form as a service.

#4

See also this answer: stackoverflow.com/questions/28421153/…

#5

I'm using silex and doctrine dbal, so no ORM features available. Project is complex, I need as much freedom as I can get. Your approach is related to modifying entity or model part of application, that is what I want to avoid. I need to pass data which is available to me in controller to set method of my League object which is called by Symfony\...\Form. Apparently there is no neat way to do it. I'll try to make a dependecy injection in League's constructor. Thanks for input!

#6

@JamilNajafov Can't you just pass your data in the $options array of your buildForm?

#7

I can pass it, and it works well. How do you manage to make it passed to setInformation method when form is submitted?