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

Skip to content

[Validator] Type Properties not working in Constraints #37387

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

Closed
nico-incubiq opened this issue Jun 22, 2020 · 5 comments
Closed

[Validator] Type Properties not working in Constraints #37387

nico-incubiq opened this issue Jun 22, 2020 · 5 comments

Comments

@nico-incubiq
Copy link
Contributor

Symfony version(s) affected: 5.1.2 (and others too, as long as you're using PHP 7.4 and typed properties)

Description
PHP 7.4 introduced typed properties, which seem to be the way forward to build type-safe applications. But due to the way the Constraint component works to validate its options, it is not possible to use typed properties in a Constraint.

How to reproduce

class PostCode extends Constraint {
    public string $countryCode; // This is not initialised yet.
}

/// later ///

new PostCode(['countryCode' => 'CA']); // This throws an exception saying "countryCode" is not a valid option

Possible Solution
The issue is due to the Constraint class using get_object_vars($this) in its constructor to find all the available properties on the class, which won't return the uninitialised ones, as explained here https://bugs.php.net/bug.php?id=78954. One solution is to use Reflection to find out what properties are available.

@nico-incubiq
Copy link
Contributor Author

I've actually tried to use the Reflection API to fix this but it didn't help...

diff --git a/src/Symfony/Component/Validator/Constraint.php b/src/Symfony/Component/Validator/Constraint.php
index f967055211..929c5c65d5 100644
--- a/src/Symfony/Component/Validator/Constraint.php
+++ b/src/Symfony/Component/Validator/Constraint.php
@@ -107,7 +107,9 @@ abstract class Constraint
     {
         $invalidOptions = array();
         $missingOptions = array_flip((array) $this->getRequiredOptions());
-        $knownOptions = get_object_vars($this);
+        $knownOptions = array_map(function (\ReflectionProperty $property): string {
+            return $property->getName();
+        }, (new \ReflectionClass($this))->getProperties());
 
         // The "groups" option is added to the object lazily
         $knownOptions['groups'] = true;
~

@xabbuh
Copy link
Member

xabbuh commented Jun 23, 2020

see #37392

@fabpot fabpot closed this as completed Jun 25, 2020
fabpot added a commit that referenced this issue Jun 25, 2020
…tions (xabbuh)

This PR was merged into the 3.4 branch.

Discussion
----------

[Validator] fix handling typed properties as constraint options

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37387
| License       | MIT
| Doc PR        |

Commits
-------

4a66a60 fix handling typed properties as constraint options
@nico-incubiq
Copy link
Contributor Author

nico-incubiq commented Jun 25, 2020

@fabpot this also need to go into all newer versions after 3.4, what's the process? thanks

@xabbuh
Copy link
Member

xabbuh commented Jun 25, 2020

@nico-incubiq Branches are regularly merged up into all other maintained branches.

@nico-incubiq
Copy link
Contributor Author

Great, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants