-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add support of nested attributes #41994
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
fabpot
merged 1 commit into
symfony:5.4
from
alexandre-daubois:feat-nested-attributes-support
Aug 18, 2021
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,16 @@ | |
* | ||
* @author Bernhard Schussek <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class All extends Composite | ||
{ | ||
public $constraints = []; | ||
|
||
public function __construct($constraints = null, array $groups = null, $payload = null) | ||
{ | ||
parent::__construct($constraints ?? [], $groups, $payload); | ||
} | ||
|
||
public function getDefaultOption() | ||
{ | ||
return 'constraints'; | ||
|
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
* | ||
* @author Przemysław Bogusz <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class AtLeastOneOf extends Composite | ||
{ | ||
public const AT_LEAST_ONE_OF_ERROR = 'f27e6d6c-261a-4056-b391-6673a623531c'; | ||
|
@@ -30,6 +31,15 @@ class AtLeastOneOf extends Composite | |
public $messageCollection = 'Each element of this collection should satisfy its own set of constraints.'; | ||
public $includeInternalMessages = true; | ||
|
||
public function __construct($constraints = null, array $groups = null, $payload = null, string $message = null, string $messageCollection = null, bool $includeInternalMessages = null) | ||
{ | ||
parent::__construct($constraints ?? [], $groups, $payload); | ||
|
||
$this->message = $message ?? $this->message; | ||
$this->messageCollection = $messageCollection ?? $this->messageCollection; | ||
$this->includeInternalMessages = $includeInternalMessages ?? $this->includeInternalMessages; | ||
} | ||
|
||
public function getDefaultOption() | ||
{ | ||
return 'constraints'; | ||
|
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 |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
* | ||
* @author Bernhard Schussek <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Collection extends Composite | ||
{ | ||
public const MISSING_FIELD_ERROR = '2fa2158c-2a7f-484b-98aa-975522539ff8'; | ||
|
@@ -38,15 +39,20 @@ class Collection extends Composite | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct($options = null) | ||
public function __construct($fields = null, array $groups = null, $payload = null, bool $allowExtraFields = null, bool $allowMissingFields = null, string $extraFieldsMessage = null, string $missingFieldsMessage = null) | ||
{ | ||
// no known options set? $options is the fields array | ||
if (\is_array($options) | ||
&& !array_intersect(array_keys($options), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) { | ||
$options = ['fields' => $options]; | ||
// no known options set? $fields is the fields array | ||
if (\is_array($fields) | ||
&& !array_intersect(array_keys($fields), ['groups', 'fields', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage'])) { | ||
$fields = ['fields' => $fields]; | ||
} | ||
|
||
parent::__construct($options); | ||
parent::__construct($fields, $groups, $payload); | ||
|
||
$this->allowExtraFields = $allowExtraFields ?? $this->allowExtraFields; | ||
$this->allowMissingFields = $allowMissingFields ?? $this->allowMissingFields; | ||
$this->extraFieldsMessage = $extraFieldsMessage ?? $this->extraFieldsMessage; | ||
$this->missingFieldsMessage = $missingFieldsMessage ?? $this->missingFieldsMessage; | ||
} | ||
|
||
/** | ||
|
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 |
---|---|---|
|
@@ -20,10 +20,16 @@ | |
* | ||
* @author Maxime Steinhausser <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)] | ||
class Sequentially extends Composite | ||
{ | ||
public $constraints = []; | ||
|
||
public function __construct($constraints = null, array $groups = null, $payload = null) | ||
{ | ||
parent::__construct($constraints ?? [], $groups, $payload); | ||
} | ||
|
||
public function getDefaultOption() | ||
{ | ||
return 'constraints'; | ||
|
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
172 changes: 172 additions & 0 deletions
172
src/Symfony/Component/Validator/Tests/Fixtures/NestedAttribute/Entity.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,172 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\Context\ExecutionContextInterface; | ||
use Symfony\Component\Validator\Tests\Fixtures\Attribute\EntityParent; | ||
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceB; | ||
use Symfony\Component\Validator\Tests\Fixtures\CallbackClass; | ||
use Symfony\Component\Validator\Tests\Fixtures\ConstraintA; | ||
|
||
#[ | ||
ConstraintA, | ||
Assert\GroupSequence(['Foo', 'Entity']), | ||
Assert\Callback([CallbackClass::class, 'callback']), | ||
] | ||
class Entity extends EntityParent implements EntityInterfaceB | ||
{ | ||
#[ | ||
Assert\NotNull, | ||
Assert\Range(min: 3), | ||
Assert\All([ | ||
new Assert\NotNull(), | ||
new Assert\Range(min: 3), | ||
]), | ||
Assert\All( | ||
constraints: [ | ||
new Assert\NotNull(), | ||
new Assert\Range(min: 3), | ||
], | ||
), | ||
Assert\Collection( | ||
fields: [ | ||
'foo' => [ | ||
new Assert\NotNull(), | ||
new Assert\Range(min: 3), | ||
], | ||
'bar' => new Assert\Range(min: 5), | ||
'baz' => new Assert\Required([new Assert\Email()]), | ||
'qux' => new Assert\Optional([new Assert\NotBlank()]), | ||
], | ||
allowExtraFields: true | ||
), | ||
Assert\Choice(choices: ['A', 'B'], message: 'Must be one of %choices%'), | ||
Assert\AtLeastOneOf( | ||
constraints: [ | ||
new Assert\NotNull(), | ||
new Assert\Range(min: 3), | ||
], | ||
message: 'foo', | ||
includeInternalMessages: false, | ||
), | ||
Assert\Sequentially([ | ||
new Assert\NotBlank(), | ||
new Assert\Range(min: 5), | ||
]), | ||
] | ||
public $firstName; | ||
#[Assert\Valid] | ||
public $childA; | ||
#[Assert\Valid] | ||
public $childB; | ||
protected $lastName; | ||
public $reference; | ||
public $reference2; | ||
private $internal; | ||
public $data = 'Overridden data'; | ||
public $initialized = false; | ||
|
||
public function __construct($internal = null) | ||
{ | ||
$this->internal = $internal; | ||
} | ||
|
||
public function getFirstName() | ||
{ | ||
return $this->firstName; | ||
} | ||
|
||
public function getInternal() | ||
{ | ||
return $this->internal.' from getter'; | ||
} | ||
|
||
public function setLastName($lastName) | ||
{ | ||
$this->lastName = $lastName; | ||
} | ||
|
||
#[Assert\NotNull] | ||
public function getLastName() | ||
{ | ||
return $this->lastName; | ||
} | ||
|
||
public function getValid() | ||
{ | ||
} | ||
|
||
#[Assert\IsTrue] | ||
public function isValid() | ||
{ | ||
return 'valid'; | ||
} | ||
|
||
#[Assert\IsTrue] | ||
public function hasPermissions() | ||
{ | ||
return 'permissions'; | ||
} | ||
|
||
public function getData() | ||
{ | ||
return 'Overridden data'; | ||
} | ||
|
||
#[Assert\Callback(payload: 'foo')] | ||
public function validateMe(ExecutionContextInterface $context) | ||
{ | ||
} | ||
|
||
#[Assert\Callback] | ||
public static function validateMeStatic($object, ExecutionContextInterface $context) | ||
{ | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getChildA() | ||
{ | ||
return $this->childA; | ||
} | ||
|
||
/** | ||
* @param mixed $childA | ||
*/ | ||
public function setChildA($childA) | ||
{ | ||
$this->childA = $childA; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getChildB() | ||
{ | ||
return $this->childB; | ||
} | ||
|
||
/** | ||
* @param mixed $childB | ||
*/ | ||
public function setChildB($childB) | ||
{ | ||
$this->childB = $childB; | ||
} | ||
|
||
public function getReference() | ||
{ | ||
return $this->reference; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/Symfony/Component/Validator/Tests/Fixtures/NestedAttribute/EntityParent.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,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute; | ||
|
||
use Symfony\Component\Validator\Constraints\NotNull; | ||
use Symfony\Component\Validator\Tests\Fixtures\EntityInterfaceA; | ||
|
||
class EntityParent implements EntityInterfaceA | ||
{ | ||
protected $firstName; | ||
private $internal; | ||
private $data = 'Data'; | ||
private $child; | ||
|
||
#[NotNull] | ||
protected $other; | ||
|
||
public function getData() | ||
{ | ||
return 'Data'; | ||
} | ||
|
||
public function getChild() | ||
{ | ||
return $this->child; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...ymfony/Component/Validator/Tests/Fixtures/NestedAttribute/GroupSequenceProviderEntity.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,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Fixtures\NestedAttribute; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Component\Validator\GroupSequenceProviderInterface; | ||
|
||
#[Assert\GroupSequenceProvider] | ||
class GroupSequenceProviderEntity implements GroupSequenceProviderInterface | ||
{ | ||
public $firstName; | ||
public $lastName; | ||
|
||
protected $sequence = []; | ||
|
||
public function __construct($sequence) | ||
{ | ||
$this->sequence = $sequence; | ||
} | ||
|
||
public function getGroupSequence() | ||
{ | ||
return $this->sequence; | ||
} | ||
} |
Oops, something went wrong.
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.
@chekalsky this is the test I mean