I have a problem with nesting forms from different objects in one form, my form is displayed with the name first [input] lastname [input] - but the address has no input next to it.
Basically, I want to create a form in which the user can add a name, surname, address1, address2, city, country, etc. and present it as one, although these are different tables.
The main form is not a problem, the only problem I encountered is the second inline form. Any help would be greatly appreciated.
Here is my code:
Member Class:
namespace Pomc\MembersBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Member { private $id; private $firstName; private $lastName; private $address; private $telephone; public function __construct() { $this->address = new \Doctrine\Common\Collections\ArrayCollection(); $this->telephone = new \Doctrine\Common\Collections\ArrayCollection(); } public function getId() { return $this->id; } public function setFirstName($firstName) { $this->firstName = $firstName; } public function getFirstName() { return $this->firstName; } public function setLastName($lastName) { $this->lastName = $lastName; } public function getLastName() { return $this->lastName; } public function addAddress(\Pomc\MembersBundle\Entity\Address $address) { $this->address[] = $address; } public function getAddress() { return $this->address; } public function addTelephone(\Pomc\MembersBundle\Entity\Telephone $telephone) { $this->telephone[] = $telephone; } public function getTelephone() { return $this->telephone; } }
Here is the address class:
namespace Pomc\MembersBundle\Entity; use Doctrine\ORM\Mapping as ORM; class Address { private $id; private $addressType; private $firstLine; private $secondLine; private $city; private $postCode; private $country; private $member; public function getId() { return $this->id; } public function setAddressType($addressType) { $this->addressType = $addressType; } public function getAddressType() { return $this->addressType; } public function setFirstLine($firstLine) { $this->firstLine = $firstLine; } public function getFirstLine() { return $this->firstLine; } public function setSecondLine($secondLine) { $this->secondLine = $secondLine; } public function getSecondLine() { return $this->secondLine; } public function setCity($city) { $this->city = $city; } public function getCity() { return $this->city; } public function setPostCode($postCode) { $this->postCode = $postCode; } public function getPostCode() { return $this->postCode; } public function setCountry($country) { $this->country = $country; } public function getCountry() { return $this->country; } public function setMember(\Pomc\MembersBundle\Entity\Member $member) { $this->member = $member; } public function getMember() { return $this->member; } }
Here is the form member:
namespace Pomc\MembersBundle\Form\Type; use \Symfony\Component\Form\AbstractType; use \Symfony\Component\Form\FormBuilder; class MemberType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add('firstName'); $builder->add('lastName'); $builder->add('address','collection', array( 'type' => new AddressType(), 'allow_add' => true, 'prototype' => true, 'by_reference' => false, )); } public function getDefaultOptions(array $options) { return array('data_class' => 'Pomc\MembersBundle\Entity\Member'); } function getName() { return 'member'; } }
Here is the address form:
namespace Pomc\MembersBundle\Form\Type; use \Symfony\Component\Form\AbstractType; use \Symfony\Component\Form\FormBuilder; class AddressType extends AbstractType { public function buildForm(Formbuilder $builder, array $options) { $builder->add('firstLine'); } public function getDefaultOptions(array $options) { return array('data_class' => 'Pomc\MembersBundle\Entity\Address'); } function getName() { return 'address'; } function getIdentifier() { return 'address'; } }
Here is the controller:
namespace Pomc\MembersBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use \Pomc\MembersBundle\Entity\Member; use \Symfony\Component\HttpFoundation\Request; use \Pomc\MembersBundle\Form\Type\MemberType; class DefaultController extends Controller { public function indexAction($name) { return $this->render('PomcMembersBundle:Default:index.html.twig', array('name' => $name)); } public function newAction(Request $request) { $member = new Member(); $form = $this->get('form.factory')->create(new MemberType()); if($request->getMethod() == 'POST') { $form->bindRequest($request); if($form->isValid()) { $em = $this->getDoctrine()->getEntityManager(); $em->persist($member); $em->flush(); } } return $this->render('PomcMembersBundle:Default:new.html.twig', array( 'form'=> $form->createView(),)); } }
Here is the template:
<form action="{{ path('member_new') }}" method="post" {{ form_enctype(form)}}> {{ form_widget(form) }} <div> {{ form_row(form.address)}} </div> <input type="submit" /> </form>
I have been a user of this site for a long time, but this is my first question.
thanks