Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Support form types that don't have a service (#244)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wouterj authored and soullivaneuh committed May 6, 2016
1 parent ccdb1c5 commit bf2b5bb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Form/Extension/DependencyInjectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
9 changes: 9 additions & 0 deletions Tests/Form/Extension/DependencyInjectionExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit bf2b5bb

Please sign in to comment.