Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use Symfony2 Form Component Standalone
<?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
);
?>
@seanmccleary

This comment has been minimized.

Copy link

seanmccleary commented Jul 27, 2013

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:

public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFactoryInterface $resolvedTypeFactory)

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?

@seanmccleary

This comment has been minimized.

Copy link

seanmccleary commented Jul 27, 2013

Also, I'd love to see an example of how to render this form now, especially if you're not using twig.

@mickaelandrieu

This comment has been minimized.

Copy link

mickaelandrieu commented Nov 26, 2015

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
You can’t perform that action at this time.