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

Skip to content

Commit 9ce3ff3

Browse files
committed
minor #31827 [Form][DX] Improved error message on create a form builder with invalid options (yceruto)
This PR was merged into the 4.4 branch. Discussion ---------- [Form][DX] Improved error message on create a form builder with invalid options | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - **before** > The required option "class" is missing. **after** > An error has occurred resolving the options of the form "App\Form\MyEntityType": The required option "class" is missing. Commits ------- 37c7a2b Improved error message on create a form builder with invalid options
2 parents be7b7fe + 37c7a2b commit 9ce3ff3

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/Symfony/Component/Form/ResolvedFormType.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\EventDispatcher\EventDispatcher;
1515
use Symfony\Component\Form\Exception\UnexpectedTypeException;
16+
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
1617
use Symfony\Component\OptionsResolver\OptionsResolver;
1718

1819
/**
@@ -92,7 +93,11 @@ public function getTypeExtensions()
9293
*/
9394
public function createBuilder(FormFactoryInterface $factory, $name, array $options = [])
9495
{
95-
$options = $this->getOptionsResolver()->resolve($options);
96+
try {
97+
$options = $this->getOptionsResolver()->resolve($options);
98+
} catch (ExceptionInterface $e) {
99+
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);
100+
}
96101

97102
// Should be decoupled from the specific option at some point
98103
$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(ResolvedFormType::class)
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)