From bf2b5bb35b0bf12e1987d3255688915ce1e9a686 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Fri, 6 May 2016 12:59:25 +0200 Subject: [PATCH] Support form types that don't have a service (#244) Form types don't have to have a corresponding service tagged as form.type. If they have no dependencies, the FQCN can be instantiated directly. This is especially important as most core form types are no longer registered as services since Symfony 3.1. --- Form/Extension/DependencyInjectionExtension.php | 6 +++++- .../Form/Extension/DependencyInjectionExtensionTest.php | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Form/Extension/DependencyInjectionExtension.php b/Form/Extension/DependencyInjectionExtension.php index 1b284215..04258dc2 100644 --- a/Form/Extension/DependencyInjectionExtension.php +++ b/Form/Extension/DependencyInjectionExtension.php @@ -90,7 +90,11 @@ public function getType($name) $name = self::findClass($this->mappingTypes, $name); if (!isset($this->typeServiceIds[$name])) { - throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name)); + if (class_exists($name) && in_array('Symfony\Component\Form\FormTypeInterface', class_implements($name), true)) { + return new $name(); + } else { + throw new InvalidArgumentException(sprintf('The field type "%s" is not registered with the service container.', $name)); + } } $type = $this->container->get($this->typeServiceIds[$name]); diff --git a/Tests/Form/Extension/DependencyInjectionExtensionTest.php b/Tests/Form/Extension/DependencyInjectionExtensionTest.php index f938aab7..b76dae12 100644 --- a/Tests/Form/Extension/DependencyInjectionExtensionTest.php +++ b/Tests/Form/Extension/DependencyInjectionExtensionTest.php @@ -50,6 +50,15 @@ public function testValidType() $f->getType('Symfony\Component\Form\Type\FormType'); } + public function testTypeWithoutService() + { + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + + $f = new DependencyInjectionExtension($container, array(), array(), array(), array()); + + $this->assertInstanceOf('Symfony\Component\Form\Extension\Core\Type\HiddenType', $f->getType('Symfony\Component\Form\Extension\Core\Type\HiddenType')); + } + public function testTypeExtensionsValid() { $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');