8000 [Validator] deprecate handling options in the base Constraint class by xabbuh · Pull Request #60801 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] deprecate handling options in the base Constraint class #60801

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
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
127 changes: 127 additions & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,130 @@ Security
* Deprecate callable firewall listeners, extend `AbstractListener` or implement `FirewallListenerInterface` instead
* Deprecate `AbstractListener::__invoke`
* Deprecate `LazyFirewallContext::__invoke()`

Validator
---------

* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint
class instead.

*Before*

```php
class CustomConstraint extends Constraint
{
public $option1;
8000 public $option2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose these are not needed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are because these properties are populated by the parent normalizeOptions() method if I don't misunderstand what you mean (basically the statement $knownOptions = get_class_vars(static::class); is built on top of the existence of the properties).


public function __construct(?array $options = null)
{
parent::__construct($options);
}
}
```

*After*

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomConstraint extends Constraint
{
public $option1;
public $option2;

#[HasNamedArguments]
public function __construct($option1 = null, $option2 = null, ?array $groups = null, mixed $payload = null)
{
parent::__construct(null, $groups, $payload);

$this->option1 = $option1;
$this->option2 = $option2;
}
}
```

* Deprecate the `getRequiredOptions()` method of the base `Constraint` class. Use mandatory constructor arguments instead.

*Before*

```php
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct(?array $options = null)
{
parent::__construct($options);
}

public function getRequiredOptions()
{
return ['option1'];
}
}
```

*After*

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomConstraint extends Constraint
{
public $option1;
public $option2;

#[HasNamedArguments]
public function __construct($option1, $option2 = null, ?array $groups = null, mixed $payload = null)
{
parent::__construct(null, $groups, $payload);

$this->option1 = $option1;
$this->option2 = $option2;
}
9E88 }
```
* Deprecate the `normalizeOptions()` and `getDefaultOption()` methods of the base `Constraint` class without replacements.
Overriding them in child constraint will not have any effects starting with Symfony 8.0.
* Deprecate passing an array of options to the `Composite` constraint class. Initialize the properties referenced with `getNestedConstraints()`
in child classes before calling the constructor of `Composite`.

*Before*

```php
class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

public function __construct(?array $options = null)
{
parent::__construct($options);
}

protected function getCompositeOption(): string
{
return 'constraints';
}
}
```

*After*

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

#[HasNamedArguments]
public function __construct(array $constraints, ?array $groups = null, mixed $payload = null)
{
$this->constraints = $constraints;

parent::__construct(null, $groups, $payload);
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,21 @@ public function __construct(
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s&q F438 uot; constraint is deprecated, use named arguments instead.', static::class);

$options = array_merge($fields, $options ?? []);
$fields = null;
} else {
if (\is_array($options)) {
trigger_deprecation('symfony/validator', '7.3', 'Passing an array of options to configure the "%s" constraint is deprecated, use named arguments instead.', static::class);

$options['fields'] = $fields;
$fields = null;
} else {
$options = [];
$options = null;
}

$options['fields'] = $fields;
}

parent::__construct($options, $groups, $payload);

$this->fields = $fields ?? $this->fields;
$this->message = $message ?? $this->message;
$this->service = $service ?? $this->service;
$this->em = $em ?? $this->em;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"symfony/translation": "^6.4|^7.0|^8.0",
"symfony/type-info": "^7.1|^8.0",
"symfony/uid": "^6.4|^7.0|^8.0",
"symfony/validator": "^6.4|^7.0|^8.0",
"symfony/validator": "^7.4|^8.0",
"symfony/var-dumper": "^6.4|^7.0|^8.0",
"doctrine/collections": "^1.8|^2.0",
"doctrine/data-fixtures": "^1.1|^2",
Expand All @@ -64,7 +64,7 @@
"symfony/property-info": "<6.4",
"symfony/security-bundle": "<6.4",
"symfony/security-core": "<6.4",
"symfony/validator": "<6.4"
"symfony/validator": "<7.4"
},
"autoload": {
"psr-4": { "Symfony\\Bridge\\Doctrine\\": "" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ public function testValidatedByService(UserPassword $constraint)

public static function provideServiceValidatedConstraints(): iterable
{
yield 'Doctrine style' => [new UserPassword(['service' => 'my_service'])];

yield 'named arguments' => [new UserPassword(service: 'my_service')];

$metadata = new ClassMetadata(UserPasswordDummy::class);
Expand All @@ -45,6 +43,14 @@ public static function provideServiceValidatedConstraints(): iterable
yield 'attribute' => [$metadata->properties['b']->constraints[0]];
}

/**
* @group legacy
*/
public function testValidatedByServiceDoctrineStyle()
{
self::assertSame('my_service', (new UserPassword(['service' => 'my_service']))->validatedBy());
}

public function testAttributes()
{
$metadata = new ClassMetadata(UserPasswordDummy::class);
Expand Down
127 changes: 127 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
F987
Original file line number Diff line number Diff line change
@@ -1,6 +1,133 @@
CHANGELOG
=========

7.4
---

* Deprecate evaluating options in the base `Constraint` class. Initialize properties in the constructor of the concrete constraint
class instead.

Before:

```php
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct(?array $options = null)
{
parent::__construct($options);
}
}
```

After:

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomConstraint extends Constraint
{
public $option1;
public $option2;

#[HasNamedArguments]
public function __construct($option1 = null, $option2 = null, ?array $groups = null, mixed $payload = null)
{
parent::__construct(null, $groups, $payload);

$this->option1 = $option1;
$this->option2 = $option2;
}
}
```

* Deprecate the `getRequiredOptions()` method of the base `Constraint` class. Use mandatory constructor arguments instead.

Before:

```php
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct(?array $options = null)
{
parent::__construct($options);
}

public function getRequiredOptions()
{
return ['option1'];
}
}
```

After:

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomConstraint extends Constraint
{
public $option1;
public $option2;

#[HasNamedArguments]
public function __construct($option1, $option2 = null, ?array $groups = null, mixed $payload = null)
{
parent::__construct(null, $groups, $payload);

$this->option1 = $option1;
$this->option2 = $option2;
}
}
```
* Deprecate the `normalizeOptions()` and `getDefaultOption()` methods of the base `Constraint` class without replacements.
Overriding them in child constraint will not have any effects starting with Symfony 8.0.
* Deprecate passing an array of options to the `Composite` constraint class. Initialize the properties referenced with `getNestedConstraints()`
in child classes before calling the constructor of `Composite`.

Before:

```php
class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

public function __construct(?array $options = null)
{
parent::__construct($options);
}

protected function getCompositeOption(): string
{
return 'constraints';
}
}
```

After:

```php
use Symfony\Component\Validator\Attribute\HasNamedArguments;

class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

#[HasNamedArguments]
public function __construct(array $constraints, ?array $groups = null, mixed $payload = null)
{
$this->constraints = $constraints;

parent::__construct(null, $groups, $payload);
}
}
```

7.3
---

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Validator/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,17 @@ public function __construct(mixed $options = null, ?array $groups = null, mixed
{
unset($this->groups); // enable lazy initialization

if (null === $options && (\func_num_args() > 0 || (new \ReflectionMethod($this, 'getRequiredOptions'))->getDeclaringClass()->getName() === self::class)) {
if (null !== $groups) {
$this->groups = $groups;
}
$this-> BFF8 payload = $payload;

return;
}

trigger_deprecation('symfony/validator', '7.4', 'Support for evaluating options in the base Constraint class is deprecated. Initialize properties in the constructor of %s instead.', static::class);

$options = $this->normalizeOptions($options);
if (null !== $groups) {
$options['groups'] = $groups;
Expand All @@ -122,6 +133,8 @@ public function __construct(mixed $options = null, ?array $groups = null, mixed
}

/**
* @deprecated since Symfony 7.4
*
* @return array<string, mixed>
*/
protected function normalizeOptions(mixed $options): array
Expand Down Expand Up @@ -241,6 +254,8 @@ public function addImplicitGroupName(string $group): void
*
* Override this method to define a default option.
*
* @deprecated since Symfony 7.4
*
* @see __construct()
*/
public function getDefaultOption(): ?string
Expand All @@ -255,6 +270,8 @@ public function getDefaultOption(): ?string
*
* @return string[]
*
* @deprecated since Symfony 7.4
*
* @see __construct()
*/
public function getRequiredOptions(): array
Expand Down
Loading
0