Skip to content

[Validator] Cannot use propertyPath with uninitialized properties #46347

@benjaminmal

Description

@benjaminmal

Symfony version(s) affected

<= 6.1

Description

When a constraint uses the PropertyAccess component to get a propertyPath referencing an uninitialized property, an Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException is thrown. I think unintialized properties should be ignored and considered like null values in most constraints.

It happens in:

How to reproduce

class MyObject
{
    #[GreaterThan(propertyPath: 'b')]
    public int $a;

    public int $b;
}

$myObject = new MyObject();
$myObject->a = 12;

$validator->validate($myObject); // throws UninitializedPropertyException

Possible Solution

I propose to catch Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException when getting propertyPath values and stop the validation.

See this example from AbstractComparisonValidator:

  try {
      $comparedValue = $this->getPropertyAccessor()->getValue($object, $path);
  } catch (NoSuchPropertyException $e) {
      throw new ConstraintDefinitionException(sprintf('Invalid property path "%s" provided to "%s" constraint: ', $path, get_debug_type($constraint)).$e->getMessage(), 0, $e);
+ } catch (UninitializedPropertyException $e) {
+    return;
  }

I can work on a PR 😀

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy