Skip to content

Commit 0a54d09

Browse files
committed
Fix BC for the default root form name
The block prefix is used, to match the previous behavior when using a custom block prefix.
1 parent 9b6f592 commit 0a54d09

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

src/Symfony/Component/Form/FormFactory.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,29 @@ public function createBuilder($type = 'Symfony\Component\Form\Extension\Core\Typ
7474
$typeName = $type->getName();
7575
}
7676
} elseif ($type instanceof FormTypeInterface) {
77-
// BC
78-
$typeName = $type->getName();
77+
if (method_exists($type, 'getBlockPrefix')) {
78+
// As of Symfony 3.0, the block prefix of the type is used as
79+
// default name
80+
$name = $type->getBlockPrefix();
81+
} else {
82+
// BC
83+
$typeName = $type->getName();
84+
}
7985
} elseif (is_string($type)) {
80-
// BC
81-
$typeName = $type;
86+
$resolvedType = $this->registry->getType($type);
87+
if (method_exists($resolvedType, 'getBlockPrefix')) {
88+
// As of Symfony 3.0, the block prefix of the type is used as
89+
// default name
90+
$name = $resolvedType->getBlockPrefix();
91+
} else {
92+
// BC
93+
$typeName = $type;
94+
}
8295
} else {
8396
throw new UnexpectedTypeException($type, 'string, Symfony\Component\Form\ResolvedFormTypeInterface or Symfony\Component\Form\FormTypeInterface');
8497
}
8598

99+
// BC when there is no block prefix
86100
if (null === $name) {
87101
if (false === strpos($typeName, '\\')) {
88102
// No FQCN - leave unchanged for BC

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

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,52 @@ public function testCreateThrowsUnderstandableException()
302302
$this->factory->create(new \stdClass());
303303
}
304304

305+
public function testCreateUsesBlockPrefixIfTypeGivenAsString()
306+
{
307+
$options = array('a' => '1', 'b' => '2');
308+
$resolvedOptions = array('a' => '2', 'b' => '3');
309+
310+
// the interface does not have the method, so use the real class
311+
$resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
312+
->disableOriginalConstructor()
313+
->getMock();
314+
315+
$resolvedType->expects($this->any())
316+
->method('getBlockPrefix')
317+
->willReturn('TYPE_PREFIX');
318+
319+
$this->registry->expects($this->any())
320+
->method('getType')
321+
->with('TYPE')
322+
->will($this->returnValue($resolvedType));
323+
324+
$resolvedType->expects($this->once())
325+
->method('createBuilder')
326+
->with($this->factory, 'TYPE_PREFIX', $options)
327+
->will($this->returnValue($this->builder));
328+
329+
$this->builder->expects($this->any())
330+
->method('getOptions')
331+
->will($this->returnValue($resolvedOptions));
332+
333+
$resolvedType->expects($this->once())
334+
->method('buildForm')
335+
->with($this->builder, $resolvedOptions);
336+
337+
$this->builder->expects($this->once())
338+
->method('getForm')
339+
->will($this->returnValue('FORM'));
340+
341+
$this->assertSame('FORM', $this->factory->create('TYPE', null, $options));
342+
}
343+
305344
public function testCreateUsesTypeNameIfTypeGivenAsString()
306345
{
307346
$options = array('a' => '1', 'b' => '2');
308347
$resolvedOptions = array('a' => '2', 'b' => '3');
309348
$resolvedType = $this->getMockResolvedType();
310349

311-
$this->registry->expects($this->once())
350+
$this->registry->expects($this->any())
312351
->method('getType')
313352
->with('TYPE')
314353
->will($this->returnValue($resolvedType));
@@ -339,7 +378,7 @@ public function testCreateStripsNamespaceOffTypeName()
339378
$resolvedOptions = array('a' => '2', 'b' => '3');
340379
$resolvedType = $this->getMockResolvedType();
341380

342-
$this->registry->expects($this->once())
381+
$this->registry->expects($this->any())
343382
->method('getType')
344383
->with('Vendor\Name\Space\UserForm')
345384
->will($this->returnValue($resolvedType));
@@ -370,7 +409,7 @@ public function testLegacyCreateStripsNamespaceOffTypeNameAccessByFQCN()
370409
$resolvedOptions = array('a' => '2', 'b' => '3');
371410
$resolvedType = $this->getMockResolvedType();
372411

373-
$this->registry->expects($this->once())
412+
$this->registry->expects($this->any())
374413
->method('getType')
375414
->with('userform')
376415
->will($this->returnValue($resolvedType));
@@ -401,7 +440,7 @@ public function testCreateStripsTypeSuffixOffTypeName()
401440
$resolvedOptions = array('a' => '2', 'b' => '3');
402441
$resolvedType = $this->getMockResolvedType();
403442

404-
$this->registry->expects($this->once())
443+
$this->registry->expects($this->any())
405444
->method('getType')
406445
->with('Vendor\Name\Space\UserType')
407446
->will($this->returnValue($resolvedType));
@@ -432,7 +471,7 @@ public function testCreateDoesNotStripTypeSuffixIfResultEmpty()
432471
$resolvedOptions = array('a' => '2', 'b' => '3');
433472
$resolvedType = $this->getMockResolvedType();
434473

435-
$this->registry->expects($this->once())
474+
$this->registry->expects($this->any())
436475
->method('getType')
437476
->with('Vendor\Name\Space\Type')
438477
->will($this->returnValue($resolvedType));
@@ -463,7 +502,7 @@ public function testCreateConvertsTypeToUnderscoreSyntax()
463502
$resolvedOptions = array('a' => '2', 'b' => '3');
464503
$resolvedType = $this->getMockResolvedType();
465504

466-
$this->registry->expects($this->once())
505+
$this->registry->expects($this->any())
467506
->method('getType')
468507
->with('Vendor\Name\Space\MyProfileHTMLType')
469508
->will($this->returnValue($resolvedType));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy