Skip to content

Instantly share code, notes, and snippets.

@patashnik
Created April 8, 2011 09:55
Show Gist options
  • Save patashnik/909579 to your computer and use it in GitHub Desktop.
Save patashnik/909579 to your computer and use it in GitHub Desktop.
Experimental Form
<?php
public function someAction()
{
$factory = $this->get('form.factory');
$form = $factory->create(new ExperimentalForm(), 'form');
$request = $this->get('request');
if ($request->getMethod() === 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
return new RedirectResponse($this->generateUrl('homepage'));
}
}
return $this->render('MyBundle:Campaign:test.html.php', array(
'form' => $factory->createRenderer($form, 'php'),
));
}
<?php
namespace My\Company\Form;
use Symfony\Component\Form\Type\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ExperimentalForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('fields', 'collection', array(
'modifiable' => true,
'type' => new ExperimentalFormType()
));
}
}
<?php
namespace My\Company\Form;
use Symfony\Component\Form\Type\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ExperimentalFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('field', 'text')
->add('secondField', 'text');
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'My\Company\Entity\Field'
);
}
}
<?php
namespace My\Company\Entity;
class Field
{
/**
* @assert:NotBlank()
* @assert:Min(2)
*/
private $field;
/**
* @assert:NotBlank()
* @assert:Min(2)
*/
private $secondField;
public function getField()
{
return $this->field;
}
public function setField($field)
{
$this->field = $field;
}
public function getSecondField()
{
return $this->secondField;
}
public function setSecondField($field)
{
$this->secondField = $field;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment