Skip to content

Commit

Permalink
minor #31827 [Form][DX] Improved error message on create a form build…
Browse files Browse the repository at this point in the history
…er 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
  • Loading branch information
fabpot committed Jun 4, 2019
2 parents be7b7fe + 37c7a2b commit 9ce3ff3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Symfony/Component/Form/ResolvedFormType.php
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand Down Expand Up @@ -92,7 +93,11 @@ public function getTypeExtensions()
*/
public function createBuilder(FormFactoryInterface $factory, $name, array $options = [])
{
$options = $this->getOptionsResolver()->resolve($options);
try {
$options = $this->getOptionsResolver()->resolve($options);
} catch (ExceptionInterface $e) {
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);
}

// Should be decoupled from the specific option at some point
$dataClass = isset($options['data_class']) ? $options['data_class'] : null;
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Form/Tests/ResolvedFormTypeTest.php
Expand Up @@ -179,6 +179,33 @@ public function testCreateBuilderWithDataClassOption()
$this->assertSame('\stdClass', $builder->getDataClass());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
* @expectedExceptionMessage An error has occurred resolving the options of the form "stdClass": The required option "foo" is missing.
*/
public function testFailsCreateBuilderOnInvalidFormOptionsResolution()
{
$optionsResolver = (new OptionsResolver())
->setRequired('foo')
;
$this->resolvedType = $this->getMockBuilder(ResolvedFormType::class)
->setConstructorArgs([$this->type, [$this->extension1, $this->extension2], $this->parentResolvedType])
->setMethods(['getOptionsResolver', 'getInnerType'])
->getMock()
;
$this->resolvedType->expects($this->once())
->method('getOptionsResolver')
->willReturn($optionsResolver)
;
$this->resolvedType->expects($this->once())
->method('getInnerType')
->willReturn(new \stdClass())
;
$factory = $this->getMockFormFactory();

$this->resolvedType->createBuilder($factory, 'name');
}

public function testBuildForm()
{
$i = 0;
Expand Down

0 comments on commit 9ce3ff3

Please sign in to comment.