Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Form] made sure the child type default options are passed to the parent when... #3512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function getDefaultOptions(array $options)
'query_builder' => null,
'loader' => null,
'group_by' => null,
'choices' => null,
);

$options = array_replace($defaultOptions, $options);
Expand Down
17 changes: 12 additions & 5 deletions 17 src/Symfony/Component/Form/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,31 +222,38 @@ public function createNamedBuilder($type, $name, $data = null, array $options =
$defaultOptions = array();
$optionValues = array();
$passedOptions = $options;
$parentOptions = $options;

// Bottom-up determination of the type hierarchy
// Start with the actual type and look for the parent type
// The complete hierarchy is saved in $types, the first entry being
// the root and the last entry being the leaf (the concrete type)
while (null !== $type) {
if ($type instanceof FormTypeInterface) {
if ($type->getName() == $type->getParent($options)) {
throw new FormException(sprintf('The form type name "%s" for class "%s" cannot be the same as the parent type.', $type->getName(), get_class($type)));
}

$this->addType($type);
} elseif (is_string($type)) {
$type = $this->getType($type);
} else {
throw new UnexpectedTypeException($type, 'string or Symfony\Component\Form\FormTypeInterface');
}

$parentOptions = array_replace($type->getDefaultOptions($parentOptions), $parentOptions);
foreach ($type->getExtensions() as $typeExtension) {
$parentOptions = array_replace($typeExtension->getDefaultOptions($parentOptions), $parentOptions);
}

$parentType = $type->getParent($parentOptions);
if ($type->getName() == $parentType) {
throw new FormException(sprintf('The form type name "%s" for class "%s" cannot be the same as the parent type.', $type->getName(), get_class($type)));
}

array_unshift($types, $type);

// getParent() cannot see default options set by this type nor
// default options set by parent types
// As a result, the options always have to be checked for
// existence with isset() before using them in this method.
$type = $type->getParent($options);
$type = $parentType;
}

// Top-down determination of the options and default options
Expand Down
26 changes: 26 additions & 0 deletions 26 tests/Symfony/Tests/Component/Form/Fixtures/FooChildType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Symfony\Tests\Component\Form\Fixtures;

use Symfony\Component\Form\AbstractType;

class FooChildType extends AbstractType
{

public function getName()
{
return 'foo_child';
}

public function getParent(array $options)
{
return new FooType();
}

public function getDefaultOptions(array $options)
{
return array(
'parent' => new FooParentType()
);
}
}
19 changes: 19 additions & 0 deletions 19 tests/Symfony/Tests/Component/Form/Fixtures/FooParentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Symfony\Tests\Component\Form\Fixtures;

use Symfony\Component\Form\AbstractType;

class FooParentType extends AbstractType
{

public function getName()
{
return 'foo_parent';
}

public function getParent(array $options)
{
return null;
}
}
3 changes: 2 additions & 1 deletion 3 tests/Symfony/Tests/Component/Form/Fixtures/FooType.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function getDefaultOptions(array $options)
'required' => false,
'max_length' => null,
'a_or_b' => 'a',
'parent' => null
);
}

Expand All @@ -44,6 +45,6 @@ public function getAllowedOptionValues(array $options)

public function getParent(array $options)
{
return null;
return $options['parent'];
}
}
12 changes: 12 additions & 0 deletions 12 tests/Symfony/Tests/Component/Form/FormFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Tests\Component\Form\Fixtures\TestExtension;
use Symfony\Tests\Component\Form\Fixtures\FooType;
use Symfony\Tests\Component\Form\Fixtures\FooChildType;
use Symfony\Tests\Component\Form\Fixtures\FooTypeBarExtension;
use Symfony\Tests\Component\Form\Fixtures\FooTypeBazExtension;

Expand Down Expand Up @@ -530,6 +531,17 @@ public function testUnknownOption()
);
$factory->createNamedBuilder($type, "text", "value", array("unknown" => "opt"));
}

public function testChildDefaultOptionIsPassedToChildsGetParentMethod()
{
$type = new FooChildType();

$builder = $this->factory->createNamedBuilder($type, 'foo_child');
$this->assertEquals(3, count($builder->getTypes()));

$builder = $this->factory->createNamedBuilder($type, 'foo_child', null, array('parent'=>null));
$this->assertEquals(2, count($builder->getTypes()));
}

private function createMockFactory(array $methods = array())
{
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.