Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Form with Form Collection....
<?php
namespace Test\VehicleBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LicensesFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('year', 'number', array('required' => false))
->add('make', 'text', array('required' => false))
->add('model', 'text', array('required' => false))
->add('license', 'text', array('required' => false));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Test\VehicleBundle\Entity\Vehicle'
));
}
public function getName()
{
return 'license';
}
}
<?php
namespace Test\VehicleBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LicenseFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('licenses', 'collection',
array('type' => new LicensesFormType(),
'options' => array('required' => false)
));
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Test\VehicleBundle\Entity\Vehicle'
));
}
public function getName()
{
return 'license';
}
}
/**
* Displays all license plates for a vehicle entity.
*
* @Route("/licenses")
* @Breadcrumb("Update License Plates")
* @Template
* @Method({"GET", "POST"})
*/
public function licensesAction(Request $request)
{
$vehicles = $this->getDoctrine()->getRepository('VehicleBundle:Vehicle')->findAll();
$licenseCollection = new Vehicle();
foreach ($vehicles as $vehicle) {
$licenseCollection->getLicenses()->add($vehicle);
}
$collection = $this->createForm(new LicenseFormType(), $licenseCollection);
$collection->handleRequest($request);
if ($collection->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($licenseCollection);
$em->flush();
$session = $this->getRequest()->getSession();
$session->getFlashBag()->add('success', 'License Plates updated!');
}
return array(
'forms' => $collection->createView()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment