diff --git a/src/Symfony/Component/Form/FormFactory.php b/src/Symfony/Component/Form/FormFactory.php index 3b4b075b08..2d06e59be4 100644 --- a/src/Symfony/Component/Form/FormFactory.php +++ b/src/Symfony/Component/Form/FormFactory.php @@ -92,7 +92,10 @@ class FormFactory implements FormFactoryInterface */ public function createBuilderForProperty($class, $property, $data = null, array $options = array(), FormBuilderInterface $parent = null) { - $guesser = $this->registry->getTypeGuesser(); + if (null === $guesser = $this->registry->getTypeGuesser()) { + return $this->createNamedBuilder($property, 'text', $data, $options, $parent); + } + $typeGuess = $guesser->guessType($class, $property); $maxLengthGuess = $guesser->guessMaxLength($class, $property); // Keep $minLengthGuess for BC until Symfony 2.3 diff --git a/src/Symfony/Component/Form/FormFactoryBuilder.php b/src/Symfony/Component/Form/FormFactoryBuilder.php index 65f697902c..10383e841c 100644 --- a/src/Symfony/Component/Form/FormFactoryBuilder.php +++ b/src/Symfony/Component/Form/FormFactoryBuilder.php @@ -145,9 +145,11 @@ class FormFactoryBuilder implements FormFactoryBuilderInterface $extensions = $this->extensions; if (count($this->types) > 0 || count($this->typeExtensions) > 0 || count($this->typeGuessers) > 0) { - $typeGuesser = count($this->typeGuessers) > 1 - ? new FormTypeGuesserChain($this->typeGuessers) - : $this->typeGuessers[0]; + if (count($this->typeGuessers) > 1) { + $typeGuesser = new FormTypeGuesserChain($this->typeGuessers); + } else { + $typeGuesser = isset($this->typeGuessers[0]) ? $this->typeGuessers[0] : null; + } $extensions[] = new PreloadedExtension($this->types, $this->typeExtensions, $typeGuesser); } diff --git a/src/Symfony/Component/Form/FormRegistry.php b/src/Symfony/Component/Form/FormRegistry.php index a4146dbff3..7823a77f01 100644 --- a/src/Symfony/Component/Form/FormRegistry.php +++ b/src/Symfony/Component/Form/FormRegistry.php @@ -33,9 +33,9 @@ class FormRegistry implements FormRegistryInterface private $types = array(); /** - * @var FormTypeGuesserInterface + * @var FormTypeGuesserInterface|false|null */ - private $guesser; + private $guesser = false; /** * @var ResolvedFormTypeFactoryInterface @@ -158,7 +158,7 @@ class FormRegistry implements FormRegistryInterface */ public function getTypeGuesser() { - if (null === $this->guesser) { + if (false === $this->guesser) { $guessers = array(); foreach ($this->extensions as $extension) { @@ -170,7 +170,7 @@ class FormRegistry implements FormRegistryInterface } } - $this->guesser = new FormTypeGuesserChain($guessers); + $this->guesser = !empty($guessers) ? new FormTypeGuesserChain($guessers) : null; } return $this->guesser; diff --git a/src/Symfony/Component/Form/FormRegistryInterface.php b/src/Symfony/Component/Form/FormRegistryInterface.php index 1dcf7dc384..49915ad68a 100644 --- a/src/Symfony/Component/Form/FormRegistryInterface.php +++ b/src/Symfony/Component/Form/FormRegistryInterface.php @@ -55,7 +55,7 @@ interface FormRegistryInterface /** * Returns the guesser responsible for guessing types. * - * @return FormTypeGuesserInterface + * @return FormTypeGuesserInterface|null */ public function getTypeGuesser(); diff --git a/src/Symfony/Component/Form/PreloadedExtension.php b/src/Symfony/Component/Form/PreloadedExtension.php index 8a4eb5f319..9219d8f22d 100644 --- a/src/Symfony/Component/Form/PreloadedExtension.php +++ b/src/Symfony/Component/Form/PreloadedExtension.php @@ -38,11 +38,11 @@ class PreloadedExtension implements FormExtensionInterface /** * Creates a new preloaded extension. * - * @param array $types The types that the extension should support. - * @param array $typeExtensions The type extensions that the extension should support. - * @param FormTypeGuesserInterface $typeGuesser The guesser that the extension should support. + * @param array $types The types that the extension should support. + * @param array $typeExtensions The type extensions that the extension should support. + * @param FormTypeGuesserInterface|null $typeGuesser The guesser that the extension should support. */ - public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser) + public function __construct(array $types, array $typeExtensions, FormTypeGuesserInterface $typeGuesser = null) { $this->types = $types; $this->typeExtensions = $typeExtensions; diff --git a/src/Symfony/Component/Form/Tests/FormFactoryBuilderTest.php b/src/Symfony/Component/Form/Tests/FormFactoryBuilderTest.php new file mode 100644 index 0000000000..a1292dbe72 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/FormFactoryBuilderTest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests; + +use Symfony\Component\Form\FormFactoryBuilder; +use Symfony\Component\Form\Tests\Fixtures\FooType; + +class FormFactoryBuilderTest extends \PHPUnit_Framework_TestCase +{ + private $registry; + private $guesser; + private $type; + + protected function setUp() + { + $factory = new \ReflectionClass('Symfony\Component\Form\FormFactory'); + $this->registry = $factory->getProperty('registry'); + $this->registry->setAccessible(true); + + $this->guesser = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface'); + $this->type = new FooType; + } + + public function testAddType() + { + $factoryBuilder = new FormFactoryBuilder; + $factoryBuilder->addType($this->type); + + $factory = $factoryBuilder->getFormFactory(); + $registry = $this->registry->getValue($factory); + $extensions = $registry->getExtensions(); + + $this->assertCount(1, $extensions); + $this->assertTrue($extensions[0]->hasType($this->type->getName())); + $this->assertNull($extensions[0]->getTypeGuesser()); + } + + public function testAddTypeGuesser() + { + $factoryBuilder = new FormFactoryBuilder; + $factoryBuilder->addTypeGuesser($this->guesser); + + $factory = $factoryBuilder->getFormFactory(); + $registry = $this->registry->getValue($factory); + $extensions = $registry->getExtensions(); + + $this->assertCount(1, $extensions); + $this->assertNotNull($extensions[0]->getTypeGuesser()); + } +} diff --git a/src/Symfony/Component/Form/Tests/FormFactoryTest.php b/src/Symfony/Component/Form/Tests/FormFactoryTest.php index 86a5f782b8..105e6b6663 100644 --- a/src/Symfony/Component/Form/Tests/FormFactoryTest.php +++ b/src/Symfony/Component/Form/Tests/FormFactoryTest.php @@ -348,6 +348,24 @@ class FormFactoryTest extends \PHPUnit_Framework_TestCase $this->assertSame('FORM', $this->factory->createNamed('name', 'type', null, $options)); } + public function testCreateBuilderForPropertyWithoutTypeGuesser() + { + $registry = $this->getMock('Symfony\Component\Form\FormRegistryInterface'); + $factory = $this->getMockBuilder('Symfony\Component\Form\FormFactory') + ->setMethods(array('createNamedBuilder')) + ->setConstructorArgs(array($registry, $this->resolvedTypeFactory)) + ->getMock(); + + $factory->expects($this->once()) + ->method('createNamedBuilder') + ->with('firstName', 'text', null, array()) + ->will($this->returnValue('builderInstance')); + + $builder = $factory->createBuilderForProperty('Application\Author', 'firstName'); + + $this->assertEquals('builderInstance', $builder); + } + public function testCreateBuilderForPropertyCreatesFormWithHighestConfidence() { $this->guesser1->expects($this->once()) diff --git a/src/Symfony/Component/Form/Tests/FormRegistryTest.php b/src/Symfony/Component/Form/Tests/FormRegistryTest.php index f2d4009048..746d68f3aa 100644 --- a/src/Symfony/Component/Form/Tests/FormRegistryTest.php +++ b/src/Symfony/Component/Form/Tests/FormRegistryTest.php @@ -252,6 +252,12 @@ class FormRegistryTest extends \PHPUnit_Framework_TestCase $expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2)); $this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser()); + + $registry = new FormRegistry( + array($this->getMock('Symfony\Component\Form\FormExtensionInterface')), + $this->resolvedTypeFactory); + + $this->assertNull($registry->getTypeGuesser()); } public function testGetExtensions()