Combined Forms and Defaults
Hello.
Now I would like to make life easier for all those people who write their projects based on the Symfony 2 framework .
In Symfony 2, there are great tools for creating forms from entities, as well as, more interestingly, merged forms .
So, if you need to set the default values from entities (objects) in such a (unified) form, then you (like me) will wander around the Internet, looking for a solution to this problem, because it seems to be not in the documentation (in any case, I did not find it).
And so ... I generated the form on the fly (in the controller) as follows:
In this case, I could not set the default values from the User and Client objects (it is also worth noting that “User one to many Clients” is connected between these objects). After a long search, I came across only half the solution to the problem, and I managed the rest myself. So, the solution itself:
We see that instead of createFormBuilder , I use directly createForm , into which the type of the form and the object are transferred (from the object, in fact, the default values are taken). After that, another one is attached to the same form using the add method , but $ user is no longer transferred to it, because from $ client have a method getUser () , for this I mentioned the relationship between entities.
I hope I saved someone time. Use Symfony 2 and talk about your decisions, then it will be easier for all of us to do our job =)
Read the same: Sluggable, Timestampable and so on in Symfony 2
Now I would like to make life easier for all those people who write their projects based on the Symfony 2 framework .
In Symfony 2, there are great tools for creating forms from entities, as well as, more interestingly, merged forms .
So, if you need to set the default values from entities (objects) in such a (unified) form, then you (like me) will wander around the Internet, looking for a solution to this problem, because it seems to be not in the documentation (in any case, I did not find it).
And so ... I generated the form on the fly (in the controller) as follows:
$client = $this->getDoctrine()->getRepository('CarrierUserBundle:Client')
->findOneBy(array('id' => (int) $id));
$form = $this->createFormBuilder()->add('user', new UserType())
->add('client', new ClientType())
->getForm();
In this case, I could not set the default values from the User and Client objects (it is also worth noting that “User one to many Clients” is connected between these objects). After a long search, I came across only half the solution to the problem, and I managed the rest myself. So, the solution itself:
$client = $this->getDoctrine()->getRepository('CarrierUserBundle:Client')
->findOneBy(array('id' => (int) $id));
$form = $this->createForm(new Form\ClientType(), $client)
->add($this->createForm(new Form\UserType()));
We see that instead of createFormBuilder , I use directly createForm , into which the type of the form and the object are transferred (from the object, in fact, the default values are taken). After that, another one is attached to the same form using the add method , but $ user is no longer transferred to it, because from $ client have a method getUser () , for this I mentioned the relationship between entities.
I hope I saved someone time. Use Symfony 2 and talk about your decisions, then it will be easier for all of us to do our job =)
Read the same: Sluggable, Timestampable and so on in Symfony 2