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

Skip to content

[Validator] Cannot use propertyPath with uninitialized propertiesΒ #46347

Closed
@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