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

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
118 changes: 118 additions & 0 deletions UPGRADE-7.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,121 @@ HttpClient
----------

* Deprecate using amphp/http-client < 5

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;
public $option2;

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

After:

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

public function __construct($option1 = null, $option2 = null)
{
parent::__construct();

$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
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct($option1, $option2 = null)
{
parent::__construct();

$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
class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

public function __construct(array $constraints)
{
$this->constraints = $constraints;

parent::__construct();
}
}
```
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" 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
118 changes: 118 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,124 @@
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
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct($option1 = null, $option2 = null)
{
parent::__construct();

$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
class CustomConstraint extends Constraint
{
public $option1;
public $option2;

public function __construct($option1, $option2 = null)
{
parent::__construct();

$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
class CustomCompositeConstraint extends Composite
{
public array $constraints = [];

public function __construct(array $constraints)
{
$this->constraints = $constraints;

parent::__construct();
}
}
```

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->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
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,15 @@ public function __construct(mixed $value = null, ?string $propertyPath = null, ?
} elseif (null !== $value) {
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);
} else {
$options = [];
}

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

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

$this->message = $message ?? $this->message;
$this->value = $value ?? $this->value;
$this->propertyPath = $propertyPath ?? $this->propertyPath;

if (null === $this->value && null === $this->propertyPath) {
Expand All @@ -64,6 +63,9 @@ public function __construct(mixed $value = null, ?string $propertyPath = null, ?
}
}

/**
* @deprecated since Symfony 7.4
*/
public function getDefaultOption(): ?string
{
return 'value';
Expand Down
Loading
Loading