Skip to content

Instantly share code, notes, and snippets.

@webmozart
Created January 31, 2011 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webmozart/804147 to your computer and use it in GitHub Desktop.
Save webmozart/804147 to your computer and use it in GitHub Desktop.
<?php
class AuthorForm extends Form
{
protected function configure()
{
$this->setDataClass('Application\Entities\Author');
// 1. problem
// works if this form has a field factory
$this->add('firstName');
$this->add('lastName');
$this->add('status');
// does not work, form has no field factory!
$addressForm = new Form('address');
$addressForm->add('street');
$addressForm->add('zipCode');
// 2. possible solution
// the builder would have to introspect "address" to find out
// the expected class (e.g. AssertType constraint, Doctrine relation)
// - how to set data class if the above is not possible?
// data class must already be set in the constructor, otherwise
// the nested configure() method with nested add() calls fails!
// - how to set form class?
$form = $this->getFormBuilder();
$addressForm = $builder->buildForm('address');
$addressForm->add('street');
$addressForm->add('zipCode');
// 3. possible solution
// builder introspects "address" to find data class
// add() is removed from the interface of Form - fields can only
// be added inside configure() or by using a builder
// 3.a) create Form object - use builder
// people have to learn a new concept?
$form = $this->createFormBuilder('address')
->add('street')
->add('zipCode')
->buildForm();
// 3.b) create custom form - pass builder
// how to avoid the unnecessary complexity?
$form = new AddressForm('address', array('builder' => $builder));
// configure() uses $this->add() and uses the $builder for constructing
// fields
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment