6

I have a form that I am building using the FormBuilder:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('rosters',   'entity',   array(
            'class' => 'ReliefAppsPlatformBundle:Roster',
            'property' => 'display',
            'query_builder' => function(RosterRepository $r) use ($user) {
                return $r->createQueryBuilder('r')
                    ->leftJoin('r.members', 'm')
                    ->addSelect('m')
                    ->where('(m.rights = :adminRight or m.rights = :managerRight) and m.user = :user')
                    ->setParameter('adminRight', RosterMember::ADMIN_LEVEL)
                    ->setParameter('managerRight', RosterMember::MANAGER_LEVEL)
                    ->setParameter('user', $user);
            },
            'required' => true,
            'expanded' => true,
            'multiple' => true
        ))
        ->add('save', 'submit')
    ;
}

As you can see I my QueryBuilder I use $user (the current user) as a parameter. My controler looks like that:

public function createAction(Request $request, Event $event)
{
    $alert = new RosterEvent;
    $alert->setEvent($event);

    $user = $this->getUser();
    $form = $this->createForm(new RosterEventType(), $alert, $user);
    $form->handleRequest($request);
    if ($form->isValid()) { ....

My issue is that I need to pass the $user to the formbiulder. But I get "Catchable Fatal Error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createForm() must be of the type array, object given,..." I know the problem is how I pass $user from the controller to the formbuilder. But I have no clue how to do that. Any ideas?

Raphael_b
  • 1,470
  • 3
  • 16
  • 30
  • try passing the id of the user as follow: `$user->getId()` – Matteo Jul 15 '15 at 13:53
  • try `->setParameter('user', $user->getId());` – Matteo Jul 15 '15 at 13:57
  • Thanks @Matteo; I tried but I get Catchable Fatal Error: Argument 3 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createForm() must be of the type array, integer given, – Raphael_b Jul 15 '15 at 14:03

2 Answers2

5

As mentioned in the documentation, the third parameter ($options = array()) of method createForm need an array and not a object.

This line is wrong

$form = $this->createForm(new RosterEventType(), $alert, $user);

The $options parameter can be used for example like this

$form = $this->createForm(new TaskType(), $task, array(
    'action' => $this->generateUrl('target_route'),
    'method' => 'GET',
));

If you want pass a parameter to the form, you can try something like this

Controller

$form = $this->createForm(new RosterEventType($user), $alert);

Form

protected $user;

public function __construct (User $user)
{
    $this->user = $user;
}

Hope it will help.

Community
  • 1
  • 1
zilongqiu
  • 846
  • 5
  • 12
  • Yes, that the solution, I found it yesterday but thanks a lot. Also it is necessary to __construst the variable in the RosterEventType.php (as follows): private $userId; public function __construct ($userId) { $this->userId = $userId; } – Raphael_b Jul 16 '15 at 09:37
2

The third parameter of createForm is expecting an array of options so use this when creating the form:

$form = $this->createForm(new RosterEventType(), $alert, array('user'=>$user));

Then you will need to get your RosterEventType to allow the 'user' option to be provided. You can do this by overriding the setDefaultOptions function of your AbstractType and provided a default value for the option. For example you can add the following to your RosterEventType class:

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'user' => false,
    ));
}

If you do this then you can access the user object in your buildForm function using $options['user'].

dk80
  • 556
  • 2
  • 13
  • Well done ! ;) Just for more information you can add in your post that the `setDefaultOptions ` was replaced by `configureOptions` in **Symfony 2.7** – zilongqiu Jul 15 '15 at 14:50
  • Hi I accepted @zilongqiu answer as it is the same I am using but yours is also very good. Thanks a lot!! – Raphael_b Jul 16 '15 at 09:39