Created
August 25, 2011 07:31
-
-
Save jubianchi/1170166 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 autoload function here | |
//... | |
//This is a Doctrine mapped entity class | |
//This should be placed in a seperate 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 seperate 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 defautl 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 | |
); | |
?> |
Also, I'd love to see an example of how to render this form now, especially if you're not using twig.
Hi, thanks for the hint,
I have only one question: do you have a sample of implementation without the DependencyInjection component ?
edit: found it => https://github.com/webmozart/standalone-forms :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. I have a question.
On line 103, you are instantiating a \Symfony\Component\Form\FormFactory, and passing the constructor an array.
However, the method signature for that constructor is:
How is it working with you passing it an array, if it's expecting an object of type FormRegistryInterface?
Is your code outdated? Or am I misunderstanding something?