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

Skip to content

[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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/patch-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
case false !== strpos($file, '/src/Symfony/Component/PropertyInfo/Tests/Fixtures/ParentDummy.php'):
case false !== strpos($file, '/src/Symfony/Component/Runtime/Internal/ComposerPlugin.php'):
case false !== strpos($file, '/src/Symfony/Component/Serializer/Tests/Normalizer/Features/ObjectOuter.php'):
case false !== strpos($file, '/src/Symfony/Component/Validator/Tests/Fixtures/NestedAttribute/Entity.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/NotLoadableClass.php'):
case false !== strpos($file, '/src/Symfony/Component/VarDumper/Tests/Fixtures/ReflectionIntersectionTypeFixture.php'):
continue 2;
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/All.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AtLeastOneOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
18 changes: 12 additions & 6 deletions src/Symfony/Component/Validator/Constraints/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ abstract class Composite extends Constraint
* cached. When constraints are loaded from the cache, no more group
* checks need to be done.
*/
public function __construct($options = null)
public function __construct($options = null, array $groups = null, $payload = null)
{
parent::__construct($options);
parent::__construct($options, $groups, $payload);

$this->initializeNestedConstraints();

Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Sequentially.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ class Entity extends EntityParent implements EntityInterfaceB
* @Assert\All(constraints={@Assert\NotNull, @Assert\Range(min=3)})
* @Assert\Collection(fields={
* "foo" = {@Assert\NotNull, @Assert\Range(min=3)},
* "bar" = @Assert\Range(min=5)
* })
* "bar" = @Assert\Range(min=5),
* "baz" = @Assert\Required({@Assert\Email()}),
* "qux" = @Assert\Optional({@Assert\NotBlank()})
* }, allowExtraFields=true)
* @Assert\Choice(choices={"A", "B"}, message="Must be one of %choices%")
* @Assert\AtLeastOneOf({@Assert\NotNull, @Assert\Range(min=3)}, message="foo", includeInternalMessages=false)
* @Assert\Sequentially({@Assert\NotBlank, @Assert\Range(min=5)})
*/
public $firstName;
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ class Entity extends EntityParent implements EntityInterfaceB
* @Assert\All(constraints={@Assert\NotNull, @Assert\Range(min=3)})
* @Assert\Collection(fields={
* "foo" = {@Assert\NotNull, @Assert\Range(min=3)},
* "bar" = @Assert\Range(min=5)
* })
* "bar" = @Assert\Range(min=5),
* "baz" = @Assert\Required({@Assert\Email()}),
* "qux" = @Assert\Optional({@Assert\NotBlank()})
* }, allowExtraFields=true)
* @Assert\Choice(choices={"A", "B"}, message="Must be one of %choices%")
* @Assert\AtLeastOneOf({@Assert\NotNull, @Assert\Range(min=3)}, message="foo", includeInternalMessages=false)
* @Assert\Sequentially({@Assert\NotBlank, @Assert\Range(min=5)})
*/
#[
Assert\NotNull,
Expand Down
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()]),
Copy link
Member

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

],
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;
}
}
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;
}
}
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;
}
}
Loading