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