8000 minor #15798 [2.8][Form] Fix php warning on invalid FormFactory::crea… · symfony/symfony@70fcc9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 70fcc9c

Browse files
committed
minor #15798 [2.8][Form] Fix php warning on invalid FormFactory::createBuilder() argument (xelaris)
This PR was merged into the 2.8 branch. Discussion ---------- [2.8][Form] Fix php warning on invalid FormFactory::createBuilder() argument | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Without this check it comes to a `strpos() expects parameter 1 to be string, object given` warning, when passing an invalid argument to `FormFactory::createBuilder()` (e.g. when calling `$this->createForm(new AnEntity());` instead of `$this->createForm(new AnEntityType());` in a controller). Commits ------- b5599a5 [Form] Fix php warning on invalid FormFactory::createBuilder() argument
2 parents b75755c + b5599a5 commit 70fcc9c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/Symfony/Component/Form/FormFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ
7676
} elseif ($type instanceof FormTypeInterface) {
7777
// BC
7878
$typeName = $type->getName();
79-
} else {
79+
} elseif (is_string($type)) {
8080
// BC
8181
$typeName = $type;
82+
} else {
83+
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
8284
}
8385

8486
if (null === $name) {

src/Symfony/Component/Form/Tests/FormFactoryTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ public function testCreateNamedBuilderThrowsUnderstandableException()
293293
$this->factory->createNamedBuilder('name', new \stdClass());
294294
}
295295

296+
/**
297+
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
298+
* @expectedExceptionMessage Expected argument of type "string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface", "stdClass" given
299+
*/
300+
public function testCreateThrowsUnderstandableException()
301+
{
302+
$this->factory->create(new \stdClass());
303+
}
304+
296305
public function testCreateUsesTypeNameIfTypeGivenAsString()
297306
{
298307
$options = array('a' => '1', 'b' => '2');

0 commit comments

Comments
 (0)
0