-
-
Save mickaelandrieu/7f234a374f9468d7ab6c to your computer and use it in GitHub Desktop.
How to use Symfony2 Form Component Standalone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Standalone\Form; | |
use \Symfony\Component\HttpFoundation as SHttp; | |
use \Symfony\Component\Form as SForm; | |
use \Symfony\Component\DependencyInjection as SDI; | |
use \Symfony\Bridge as SBridge; | |
//Register all your autoloaded functions here | |
//... | |
//This is a Doctrine mapped entity class | |
//This should be placed in a separate file | |
/** | |
* @Table(name="person") | |
*/ | |
class Person { | |
/** | |
* @Id | |
* @GeneratedValue | |
* @Column(type="integer") | |
* @var string | |
*/ | |
protected $id; | |
public function getId() { return $this->id; } | |
/** | |
* @Column(type="string", unique=true) | |
* @var string | |
*/ | |
protected $uid; | |
public function getUid() { return $this->uid; } | |
public function setUid($uid) { $this->uid = $uid; return $this; } | |
/** | |
* @Column(type="string") | |
* @var string | |
*/ | |
protected $firstname; | |
public function getFirstname() { return $this->firstname; } | |
public function setFirstname($firstname) { $this->firstname = $firstname; return $this; } | |
/** | |
* @Column(type="string") | |
* @var string | |
*/ | |
protected $lastname; | |
public function getLastname() { return $this->lastname; } | |
public function setLastname($lastname) { $this->lastname = $lastname; return $this; } | |
/** | |
* @Column(type="string", unique=true) | |
* @var string | |
*/ | |
protected $email; | |
public function getEmail() { return $this->email; } | |
public function setEmail($email) { $this->email = $email; return $this; } | |
} | |
//The form class | |
//This should be placed in a separate file | |
//See http://symfony.com/doc/current/book/forms.html#creating-form-classes | |
class PersonForm extends SForm\AbstractType { | |
public function buildForm(SForm\FormBuilder $builder, array $options) { | |
$builder->add('id', null, array('read_only' => true)); | |
$builder->add('uid', null, array('read_only' => true)); | |
$builder->add('firstname'); | |
$builder->add('lastname'); | |
$builder->add('email'); | |
} | |
public function getDefaultOptions(array $options) { | |
return array( | |
'data_class' => 'Standalone\Form\Person', | |
); | |
} | |
public function getName() { | |
return 'standalone'; | |
} | |
} | |
//Create a default DI container which will be used later | |
$container = new SDI\Container(new SDI\ParameterBag\ParameterBag()); | |
//Here comes Doctrine init code (connection, event manager, entity manager, ...) | |
//Each component has to be put in the container (doctrine.connection, doctrine.entity_manager, ...) | |
//... | |
//Create a Doctrine Registry | |
$registry = new SBridge\Doctrine\Registry( | |
$container, | |
array('default' => 'doctrine.connection'), | |
array('default' => 'doctrine.entity_manager'), | |
'default', | |
'default' | |
); | |
//And store it in the default container | |
$container->set('doctrine.registry', $registry); | |
//Create a default FormFactory | |
$factory = new SForm\FormFactory(array( | |
new SForm\Extension\Core\CoreExtension(), | |
new SBridge\Doctrine\Form\DoctrineOrmExtension( | |
$container->get('doctrine.registry') | |
) | |
)); | |
//And store it in the default container | |
$container->set('form.factory', $factory); | |
//Create or retrieve an Person from database | |
//Store it in the $person variable | |
//And pass this variable as the second parameter (form data) of the FormFactory::create() method | |
$person = null; | |
//Create the form | |
$form = $container->get('form.factory')->create( | |
new PersonForm( | |
$container->get('doctrine.registry') | |
), | |
$person | |
); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment