Skip to content

Instantly share code, notes, and snippets.

@jwage
Created August 25, 2010 20:35
Show Gist options
  • Save jwage/550237 to your computer and use it in GitHub Desktop.
Save jwage/550237 to your computer and use it in GitHub Desktop.
<?php
namespace Application\FrontendBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Components\Form;
class MainController extends Controller
{
public function indexAction()
{
\Symfony\Components\OutputEscaper\Escaper::markClassAsSafe('Symfony\Components\Form\Form');
$invoice = new Invoice();
$invoice->lineItems[] = new LineItem();
$invoice->lineItems[] = new LineItem();
$form = new Form\Form('invoice', $invoice, $this->container->getValidatorService());
$form->add(new Form\TextField('property1'));
$form->add(new Form\TextField('property2'));
$lineItemGroup = new Form\FieldGroup('lineItems');
$lineItemGroup->add(new Form\TextField('property1'));
$lineItemGroup->add(new Form\TextField('property2'));
$lineItemGroup->add(new Form\TextField('property3'));
$lineItemGroup->add(new Form\TextField('property4'));
$collection = new Form\CollectionField($lineItemGroup);
$form->add($collection);
if ($this->getRequest()->getMethod() == 'POST')
{
$form->bind($this->getRequest()->get('invoice'));
if ($form->isValid())
{
echo '<pre>';
print_r($invoice);
echo '</pre>';
exit('valid');
}
}
return $this->render('FrontendBundle:Main:index', array('form' => $form));
}
}
class Invoice
{
public $property1;
public $property2;
public $lineItems = array();
}
class LineItem
{
public $property1;
public $property2;
public $property3;
public $property4;
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment