ferdinandfly
ferdinandfly

Reputation: 371

symfony2 render part of form

First of all, thanks for all of you who take a look of this problem. I got a FormType like userFormType.

class UserFormType extends AbstractType{
    public function buildForm(FormBuilder $builder, array $options)
    {
         $builder->add('address','collection', ....)
                 ->add('group','entity',....)
                 ->add('companies','collection',....);
         ...
    }

}  

So you see i got two collection in the user form. I create the form and i set the companies. When i wanna to modify ONLY the information of companies and the address, but not in touch with group. So i have to render a user form, but not some company forms or address forms. so i write a controller like this:

    $user= $this->get('security.context')->getToken()->getUser();
    $form =$this->createForm(new UserForm(),$user);
    $request = $this->get('request');
    if ('POST' == $request->getMethod()) {
         $form->bindRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($user);
            $em->flush();
            ....
        }

     }

Of course, i do not wanna to modify the group so in the twig template, i dot not render the group. the form is rendered correctly, but every time i try to submit it, it tell me:

 Argument 1 passed to ... User->setGroup() must be an instance of Group ... null given

So I'm asking, what should i do?

Upvotes: 1

Views: 389

Answers (1)

MDrollette
MDrollette

Reputation: 6927

The error specifically is because your method definition in User is probably:

public function setGroup(Group $group);

but in order to set it null it would need to be:

public function setGroup(Group $group = null);

That will fix the error but it still might not be what you want functionality wise. My question is, why have the group field on the form if you are not using it? You may need another form type or pass an option to the form to not include the group field during edits.

Upvotes: 1

Related Questions