-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Adds FormErrorNormalizer #36211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.2.0 | ||
----- | ||
|
||
* added `FormErrorNormalizer` | ||
|
||
5.1.0 | ||
----- | ||
|
||
|
94 changes: 94 additions & 0 deletions
94
src/Symfony/Component/Form/Serializer/FormErrorNormalizer.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Serializer; | ||
|
||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface; | ||
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | ||
|
||
/** | ||
* Normalizes invalid Form instances. | ||
*/ | ||
final class FormErrorNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface | ||
{ | ||
const TITLE = 'title'; | ||
const TYPE = 'type'; | ||
const CODE = 'status_code'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function normalize($object, $format = null, array $context = []): array | ||
{ | ||
$data = [ | ||
'title' => $context[self::TITLE] ?? 'Validation Failed', | ||
'type' => $context[self::TYPE] ?? 'https://symfony.com/errors/form', | ||
'code' => $context[self::CODE] ?? null, | ||
'errors' => $this->convertFormErrorsToArray($object), | ||
]; | ||
|
||
if (0 !== \count($object->all())) { | ||
$data['children'] = $this->convertFormChildrenToArray($object); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supportsNormalization($data, $format = null): bool | ||
{ | ||
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); | ||
} | ||
|
||
private function convertFormErrorsToArray(FormInterface $data): array | ||
{ | ||
$errors = []; | ||
|
||
foreach ($data->getErrors() as $error) { | ||
$errors[] = [ | ||
'message' => $error->getMessage(), | ||
'cause' => $error->getCause(), | ||
]; | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function convertFormChildrenToArray(FormInterface $data): array | ||
{ | ||
$children = []; | ||
|
||
foreach ($data->all() as $child) { | ||
$childData = [ | ||
'errors' => $this->convertFormErrorsToArray($child), | ||
]; | ||
|
||
if (!empty($child->all())) { | ||
$childData['children'] = $this->convertFormChildrenToArray($child); | ||
} | ||
|
||
$children[$child->getName()] = $childData; | ||
} | ||
|
||
return $children; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasCacheableSupportsMethod(): bool | ||
{ | ||
return __CLASS__ === static::class; | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
src/Symfony/Component/Form/Tests/Serializer/FormErrorNormalizerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\Serializer; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\FormError; | ||
use Symfony\Component\Form\FormErrorIterator; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\Serializer\FormErrorNormalizer; | ||
|
||
class FormErrorNormalizerTest extends TestCase | ||
{ | ||
/** | ||
* @var FormErrorNormalizer | ||
*/ | ||
private $normalizer; | ||
|
||
/** | ||
* @var FormInterface | ||
*/ | ||
private $form; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->normalizer = new FormErrorNormalizer(); | ||
|
||
$this->form = $this->createMock(FormInterface::class); | ||
$this->form->method('isSubmitted')->willReturn(true); | ||
$this->form->method('all')->willReturn([]); | ||
|
||
$this->form->method('getErrors') | ||
->willReturn(new FormErrorIterator($this->form, [ | ||
new FormError('a', 'b', ['c', 'd'], 5, 'f'), | ||
new FormError(1, 2, [3, 4], 5, 6), | ||
]) | ||
); | ||
} | ||
|
||
public function testSupportsNormalizationWithWrongClass() | ||
{ | ||
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); | ||
} | ||
|
||
public function testSupportsNormalizationWithNotSubmittedForm() | ||
{ | ||
$form = $this->createMock(FormInterface::class); | ||
$this->assertFalse($this->normalizer->supportsNormalization($form)); | ||
} | ||
|
||
public function testSupportsNormalizationWithValidForm() | ||
{ | ||
$this->assertTrue($this->normalizer->supportsNormalization($this->form)); | ||
} | ||
|
||
public function testNormalize() | ||
{ | ||
$expected = [ | ||
'code' => null, | ||
'title' => 'Validation Failed', | ||
'type' => 'https://symfony.com/errors/form', | ||
'errors' => [ | ||
[ | ||
'message' => 'a', | ||
'cause' => 'f', | ||
], | ||
[ | ||
'message' => '1', | ||
'cause' => 6, | ||
], | ||
], | ||
]; | ||
|
||
$this->assertEquals($expected, $this->normalizer->normalize($this->form)); | ||
} | ||
|
||
public function testNormalizeWithChildren() | ||
{ | ||
$exptected = [ | ||
'code' => null, | ||
'title' => 'Validation Failed', | ||
'type' => 'https://symfony.com/errors/form', | ||
'errors' => [ | ||
[ | ||
'message' => 'a', | ||
'cause' => null, | ||
], | ||
], | ||
'children' => [ | ||
'form1' => [ | ||
'errors' => [ | ||
[ | ||
'message' => 'b', | ||
'cause' => null, | ||
], | ||
], | ||
], | ||
'form2' => [ | ||
'errors' => [ | ||
[ | ||
'message' => 'c', | ||
'cause' => null, | ||
], | ||
], | ||
'children' => [ | ||
'form3' => [ | ||
'errors' => [ | ||
[ | ||
'message' => 'd', | ||
'cause' => null, | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
$form = clone $form1 = clone $form2 = clone $form3 = $this->createMock(FormInterface::class); | ||
|
||
$form1->method('getErrors') | ||
->willReturn(new FormErrorIterator($form1, [ | ||
new FormError('b'), | ||
]) | ||
); | ||
$form1->method('getName')->willReturn('form1'); | ||
|
||
$form2->method('getErrors') | ||
->willReturn(new FormErrorIterator($form1, [ | ||
new FormError('c'), | ||
]) | ||
); | ||
$form2->method('getName')->willReturn('form2'); | ||
|
||
$form3->method('getErrors') | ||
->willReturn(new FormErrorIterator($form1, [ | ||
new FormError('d'), | ||
]) | ||
); | ||
$form3->method('getName')->willReturn('form3'); | ||
|
||
$form2->method('all')->willReturn([$form3]); | ||
|
||
$form = $this->createMock(FormInterface::class); | ||
$form->method('isSubmitted')->willReturn(true); | ||
$form->method('all')->willReturn([$form1, $form2]); | ||
$form->method('getErrors') | ||
->willReturn(new FormErrorIterator($form, [ | ||
new FormError('a'), | ||
]) | ||
); | ||
|
||
$this->assertEquals($exptected, $this->normalizer->normalize($form)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.