Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 27cae8a

Browse files
committed
Improved error message on create a form builder with invalid options
1 parent c8f3cef commit 27cae8a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/Symfony/Component/Form/ResolvedFormType.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ public function getTypeExtensions()
9292
*/
9393
public function createBuilder(FormFactoryInterface $factory, $name, array $options = [])
9494
{
95-
$options = $this->getOptionsResolver()->resolve($options);
95+
try {
96+
$options = $this->getOptionsResolver()->resolve($options);
97+
} catch (\Throwable $e) {
98+
throw new $e(sprintf('An error has occurred resolving the options of the form "%s": %s', \get_class($this->getInnerType()), $e->getMessage()), $e->getCode(), $e);
99+
}
96100

97101
// Should be decoupled from the specific option at some point
98102
$dataClass = isset($options['data_class']) ? $options['data_class'] : null;

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,33 @@ public function testCreateBuilderWithDataClassOption()
179179
$this->assertSame('\stdClass', $builder->getDataClass());
180180
}
181181

182+
/**
183+
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
184+
* @expectedExceptionMessage An error has occurred resolving the options of the form "stdClass": The required option "foo" is missing.
185+
*/
186+
public function testFailsCreateBuilderOnInvalidFormOptionsResolution()
187+
{
188+
$optionsResolver = (new OptionsResolver())
189+
->setRequired('foo')
190+
;
191+
$this->resolvedType = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormType')
192+
->setConstructorArgs([$this->type, [$this->extension1, $this->extension2], $this->parentResolvedType])
193+
->setMethods(['getOptionsResolver', 'getInnerType'])
194+
->getMock()
195+
;
196+
$this->resolvedType->expects($this->once())
197+
->method('getOptionsResolver')
198+
->willReturn($optionsResolver)
199+
;
200+
$this->resolvedType->expects($this->once())
201+
->method('getInnerType')
202+
->willReturn(new \stdClass)
203+
;
204+
$factory = $this->getMockFormFactory();
205+
206+
$this->resolvedType->createBuilder($factory, 'name');
207+
}
208+
182209
public function testBuildForm()
183210
{
184211
$i = 0;

0 commit comments

Comments
 (0)