Skip to content

Commit

Permalink
Merge remote branch 'bschussek/form'
Browse files Browse the repository at this point in the history
* bschussek/form:
  [Form] Split signature of FormFactory::create() into create() and createNamed()
  • Loading branch information
fabpot committed Apr 22, 2011
2 parents fc1ac16 + a97366f commit 4df0107
Show file tree
Hide file tree
Showing 26 changed files with 290 additions and 298 deletions.
Expand Up @@ -79,7 +79,7 @@ public function preSetData(DataEvent $event)

// Then add all rows again in the correct order
foreach ($data as $name => $value) {
$form->add($this->factory->create($this->type, $name, array(
$form->add($this->factory->createNamed($this->type, $name, null, array(
'property_path' => '['.$name.']',
)));
}
Expand Down Expand Up @@ -112,7 +112,7 @@ public function preBind(DataEvent $event)
// Add all additional rows
foreach ($data as $name => $value) {
if (!$form->has($name)) {
$form->add($this->factory->create($this->type, $name, array(
$form->add($this->factory->createNamed($this->type, $name, null, array(
'property_path' => '['.$name.']',
)));
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Form/FormBuilder.php
Expand Up @@ -364,9 +364,10 @@ public function add($name, $type = null, array $options = array())
public function build($name, $type = null, array $options = array())
{
if (null !== $type) {
$builder = $this->getFormFactory()->createBuilder(
$builder = $this->getFormFactory()->createNamedBuilder(
$type,
$name,
null,
$options
);
} else {
Expand All @@ -377,6 +378,7 @@ public function build($name, $type = null, array $options = array())
$builder = $this->getFormFactory()->createBuilderForProperty(
$this->dataClass,
$name,
null,
$options
);
}
Expand Down
53 changes: 31 additions & 22 deletions src/Symfony/Component/Form/FormFactory.php
Expand Up @@ -35,20 +35,38 @@ public function __construct(TypeLoaderInterface $typeLoader, array $guessers = a
$this->guessers = $guessers;
}

public function createBuilder($type, $name = null, array $options = array())
public function create($type, $data = null, array $options = array())
{
// TODO $type can be FQN of a type class
return $this->createBuilder($type, $data, $options)->getForm();
}

public function createNamed($type, $name, $data = null, array $options = array())
{
return $this->createNamedBuilder($type, $name, $data, $options)->getForm();
}

/**
* @inheritDoc
*/
public function createForProperty($class, $property, $data = null, array $options = array())
{
return $this->createBuilderForProperty($class, $property, $data, $options)->getForm();
}

public function createBuilder($type, $data = null, array $options = array())
{
$name = is_object($type) ? $type->getName() : $type;

return $this->createNamedBuilder($type, $name, $data, $options);
}

public function createNamedBuilder($type, $name, $data = null, array $options = array())
{
$builder = null;
$types = array();
$knownOptions = array();
$passedOptions = array_keys($options);

// TESTME
if (null === $name) {
$name = is_object($type) ? $type->getName() : $type;
}

while (null !== $type) {
// TODO check if type exists
if (!$type instanceof FormTypeInterface) {
Expand Down Expand Up @@ -80,15 +98,14 @@ public function createBuilder($type, $name = null, array $options = array())
$type->buildForm($builder, $options);
}

return $builder;
}
if (null !== $data) {
$builder->setData($data);
}

public function create($type, $name = null, array $options = array())
{
return $this->createBuilder($type, $name, $options)->getForm();
return $builder;
}

public function createBuilderForProperty($class, $property, array $options = array())
public function createBuilderForProperty($class, $property, $data = null, array $options = array())
{
// guess field class and options
$typeGuess = $this->guess(function ($guesser) use ($class, $property) {
Expand Down Expand Up @@ -121,15 +138,7 @@ public function createBuilderForProperty($class, $property, array $options = arr
$options = array_merge($typeGuess->getOptions(), $options);
}

return $this->createBuilder($type, $property, $options);
}

/**
* @inheritDoc
*/
public function createForProperty($class, $property, array $options = array())
{
return $this->createBuilderForProperty($class, $property, $options)->getForm();
return $this->createNamedBuilder($type, $property, $data, $options);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Symfony/Component/Form/FormFactoryInterface.php
Expand Up @@ -13,11 +13,15 @@

interface FormFactoryInterface
{
function createBuilder($type, $name = null, array $options = array());
function create($type, $data = null, array $options = array());

function createBuilderForProperty($class, $property, array $options = array());
function createNamed($type, $name, $data = null, array $options = array());

function create($type, $name = null, array $options = array());
function createForProperty($class, $property, $data = null, array $options = array());

function createForProperty($class, $property, array $options = array());
function createBuilder($type, $data = null, array $options = array());

function createNamedBuilder($type, $name, $data = null, array $options = array());

function createBuilderForProperty($class, $property, $data = null, array $options = array());
}
46 changes: 23 additions & 23 deletions tests/Symfony/Tests/Bridge/Doctrine/Form/EntityTypeTest.php
Expand Up @@ -80,7 +80,7 @@ protected function persist(array $entities)
//
// $this->persist(array($entity1, $entity2));
//
// $field = $this->factory->create('entity', 'name', array(
// $field = $this->factory->createNamed('entity', 'name', null, array(
// 'em' => $this->em,
// 'class' => self::SINGLE_IDENT_CLASS,
// 'required' => false,
Expand All @@ -96,7 +96,7 @@ protected function persist(array $entities)
*/
public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => new \stdClass(),
Expand All @@ -108,7 +108,7 @@ public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
*/
public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => function () {
Expand All @@ -121,7 +121,7 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()

public function testSetDataSingleNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
Expand All @@ -134,7 +134,7 @@ public function testSetDataSingleNull()

public function testSetDataMultipleExpandedNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => true,
'em' => $this->em,
Expand All @@ -148,7 +148,7 @@ public function testSetDataMultipleExpandedNull()

public function testSetDataMultipleNonExpandedNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => false,
'em' => $this->em,
Expand All @@ -162,7 +162,7 @@ public function testSetDataMultipleNonExpandedNull()

public function testSubmitSingleExpandedNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'expanded' => true,
'em' => $this->em,
Expand All @@ -176,7 +176,7 @@ public function testSubmitSingleExpandedNull()

public function testSubmitSingleNonExpandedNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'expanded' => false,
'em' => $this->em,
Expand All @@ -190,7 +190,7 @@ public function testSubmitSingleNonExpandedNull()

public function testSubmitMultipleNull()
{
$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
Expand All @@ -208,7 +208,7 @@ public function testSubmitSingleNonExpandedSingleIdentifier()

$this->persist(array($entity1, $entity2));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'expanded' => false,
'em' => $this->em,
Expand All @@ -230,7 +230,7 @@ public function testSubmitSingleNonExpandedCompositeIdentifier()

$this->persist(array($entity1, $entity2));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'expanded' => false,
'em' => $this->em,
Expand All @@ -254,7 +254,7 @@ public function testSubmitMultipleNonExpandedSingleIdentifier()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => false,
'em' => $this->em,
Expand All @@ -279,7 +279,7 @@ public function testSubmitMultipleNonExpandedSingleIdentifier_existingData()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => false,
'em' => $this->em,
Expand Down Expand Up @@ -310,7 +310,7 @@ public function testSubmitMultipleNonExpandedCompositeIdentifier()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => false,
'em' => $this->em,
Expand All @@ -336,7 +336,7 @@ public function testSubmitMultipleNonExpandedCompositeIdentifier_existingData()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => false,
'em' => $this->em,
Expand Down Expand Up @@ -366,7 +366,7 @@ public function testSubmitSingleExpanded()

$this->persist(array($entity1, $entity2));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => false,
'expanded' => true,
'em' => $this->em,
Expand All @@ -392,7 +392,7 @@ public function testSubmitMultipleExpanded()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'multiple' => true,
'expanded' => true,
'em' => $this->em,
Expand Down Expand Up @@ -424,7 +424,7 @@ public function testOverrideChoices()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
// not all persisted entities should be displayed
Expand All @@ -448,7 +448,7 @@ public function testDisallowChoicesThatAreNotIncluded_choicesSingleIdentifier()

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'choices' => array($entity1, $entity2),
Expand All @@ -469,7 +469,7 @@ public function testDisallowChoicesThatAreNotIncluded_choicesCompositeIdentifier

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::COMPOSITE_IDENT_CLASS,
'choices' => array($entity1, $entity2),
Expand All @@ -492,7 +492,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderSingleIdentifie

$repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => $repository->createQueryBuilder('e')
Expand All @@ -514,7 +514,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureSingle

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => function ($repository) {
Expand All @@ -538,7 +538,7 @@ public function testDisallowChoicesThatAreNotIncludedQueryBuilderAsClosureCompos

$this->persist(array($entity1, $entity2, $entity3));

$field = $this->factory->create('entity', 'name', array(
$field = $this->factory->createNamed('entity', 'name', null, array(
'em' => $this->em,
'class' => self::COMPOSITE_IDENT_CLASS,
'query_builder' => function ($repository) {
Expand Down

0 comments on commit 4df0107

Please sign in to comment.