-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] In Profiler, Show all fields and values for validation constraints #57963
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
Open
eltharin
wants to merge
6
commits into
symfony:7.4
Choose a base branch
from
eltharin:show_all_fields_and_values_for_validation_constraints
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−18
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
39 changes: 39 additions & 0 deletions
39
src/Symfony/Bridge/Doctrine/Tests/Fixtures/UniqueFieldFormValidationEntity.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,39 @@ | ||
<?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\Bridge\Doctrine\Tests\Fixtures; | ||
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping\Column; | ||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
|
||
#[Entity] | ||
class UniqueFieldFormValidationEntity | ||
{ | ||
public function __construct( | ||
#[Id, Column(type: 'integer')] | ||
protected ?int $id = null, | ||
|
||
#[Column(type: 'string', nullable: true)] | ||
public ?string $name = null, | ||
|
||
#[Column(type: 'string', nullable: true)] | ||
public ?string $lastname = null, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return (string) $this->name; | ||
} | ||
} |
142 changes: 142 additions & 0 deletions
142
src/Symfony/Bridge/Doctrine/Tests/Form/Validation/UniqueFieldEntityFormValidationTest.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,142 @@ | ||
<?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\Bridge\Doctrine\Tests\Form\Validation; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\Persistence\ManagerRegistry; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Bridge\Doctrine\Tests\DoctrineTestHelper; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\UniqueFieldFormValidationEntity; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator; | ||
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; | ||
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper; | ||
use Symfony\Component\Form\Test\TypeTestCase; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\FormTypeTest; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\TextTypeTest; | ||
use Symfony\Component\Validator\ConstraintValidatorFactory; | ||
use Symfony\Component\Validator\ConstraintViolation; | ||
use Symfony\Component\Validator\Validation; | ||
|
||
class UniqueFieldEntityFormValidationTest extends TypeTestCase | ||
{ | ||
private EntityManager $em; | ||
private MockObject&ManagerRegistry $emRegistry; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->em = DoctrineTestHelper::createTestEntityManager(); | ||
$this->emRegistry = $this->createRegistryMock('default', $this->em); | ||
|
||
parent::setUp(); | ||
} | ||
|
||
protected function createRegistryMock($name, $em): MockObject&ManagerRegistry | ||
{ | ||
$registry = $this->createMock(ManagerRegistry::class); | ||
$registry->expects($this->any()) | ||
->method('getManager') | ||
->with($this->equalTo($name)) | ||
->willReturn($em); | ||
|
||
return $registry; | ||
} | ||
|
||
protected function getExtensions(): array | ||
{ | ||
$factory = new ConstraintValidatorFactory([ | ||
'doctrine.orm.validator.unique' => new UniqueEntityValidator($this->emRegistry), | ||
]); | ||
|
||
$validator = Validation::createValidatorBuilder() | ||
->setConstraintValidatorFactory($factory) | ||
->enableAttributeMapping() | ||
->getValidator(); | ||
|
||
return [ | ||
new ValidatorExtension($validator), | ||
]; | ||
} | ||
|
||
public function testFormValidationForEntityWithUniqueFieldNotValid() | ||
{ | ||
$entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); | ||
|
||
$form = $this->factory | ||
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) | ||
->add('name', TextTypeTest::TESTED_TYPE) | ||
->add('token', TextTypeTest::TESTED_TYPE) | ||
->getForm(); | ||
|
||
$constraintViolation = new ConstraintViolation( | ||
'This value should not be used.', | ||
'This value should not be used.', | ||
[ | ||
'{{ value }}' => 'myNameValue', | ||
'{{ name value }}' => '"myNameValue"', | ||
], | ||
$form, | ||
'data.name', | ||
'myNameValue', | ||
null, | ||
'code', | ||
new UniqueEntity( | ||
['name'] | ||
), | ||
$entity1 | ||
); | ||
|
||
$violationMapper = new ViolationMapper(); | ||
$violationMapper->mapViolation($constraintViolation, $form, true); | ||
|
||
$this->assertCount(0, $form->getErrors()); | ||
$this->assertCount(1, $form->get('name')->getErrors()); | ||
$this->assertSame('This value should not be used.', $form->get('name')->getErrors()[0]->getMessage()); | ||
} | ||
|
||
public function testFormValidationForEntityWithUniqueGroupFieldsNotValid() | ||
{ | ||
$entity1 = new UniqueFieldFormValidationEntity(1, 'Foo'); | ||
|
||
$form = $this->factory | ||
->createNamedBuilder('parent', FormTypeTest::TESTED_TYPE, null, ['data_class' => UniqueFieldFormValidationEntity::class]) | ||
->add('name', TextTypeTest::TESTED_TYPE) | ||
->add('token', TextTypeTest::TESTED_TYPE) | ||
->getForm(); | ||
|
||
$constraintViolation = new ConstraintViolation( | ||
'This value should not be used.', | ||
'This value should not be used.', | ||
[ | ||
'{{ value }}' => 'myTokenValue, myNameValue', | ||
'{{ token value }}' => '"myTokenValue"', | ||
'{{ name value }}' => '"myNameValue"', | ||
], | ||
$form, | ||
'data.name, token', | ||
'myTokenValue, myNameValue', | ||
null, | ||
'code', | ||
new UniqueEntity( | ||
['name', 'token'] | ||
), | ||
$entity1 | ||
); | ||
|
||
$violationMapper = new ViolationMapper(); | ||
$violationMapper->mapViolation($constraintViolation, $form, true); | ||
|
||
$this->assertCount(1, $form->getErrors()); | ||
$this->assertCount(0, $form->get('name')->getErrors()); | ||
$this->assertSame('This value should not be used.', $form->getErrors()[0]->getMessage()); | ||
} | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change will break the integration into the Form component as the error now can no longer be mapped back to the corresponding form type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if one field, error stay on field, but if there are many fields, error go on top form instead one arbitrary field
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it'd be great to have a test case that covers the issue that @xabbuh spotted here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about this PR again I'd say if we were to change anything here the constraint could accept the
errorPath
option to be configured as an array which would lead to the violation being added to all these property paths. But doing the suggested change is a no go to me as it breaks the Form integration as explained.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add to all property can be OK but can create backward compatibility breaks,
but actually we have violation on only one field is not good, it's not A field whitch is bad but a group,
if you set firstname and lastname unique, form will respond to you "firstname is incorrect Christian is already used".
That's why put this to the parent form instead one field is preferable.
I think.
I'm trying to do tests and I'll look for an array (or an object stringable) for errorPath