-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator ]AbstractComparisonValidator propertyPath and NULL #29831
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
Comments
Is this the same as #29719? |
No. #29719 is about obtaining the parent object (either |
Can you try out #29839? |
@xabbuh: Should it be return null === $value2 || $value1 <= $value2; instead of return null !== $value2 && $value1 <= $value2; for |
Why would you expect it to return |
I'd assume that, if the value returned by That's why my initial proposal was to handle this generally in the |
IMO that would be unexpected behaviour for the |
That's true. What do you think would be the expected behaviour for the (greater-than/less-than)/-or-equal validators? Would you follow my assumption? |
A workaround this issue can be using the use Symfony\Component\Validator\Constraints as Assert;
class UpdateValidity
{
/**
* @Assert\Expression(
* expression="value === null or this.validUntil === null or value <= this.validUntil",
* message="This value should be less than or equal to valid until."
* )
*
* @var \DateTimeImmutable|null
*/
public $validFrom;
/**
* @Assert\Expression(
* expression="value === null or this.validFrom === null or value >= this.validFrom",
* message="This value should be greater than or equal to valid from."
* )
*
* @var \DateTimeImmutable|null
*/
public $validUntil;
} Just in case someone runs into the same problem. |
That's tricky, I think there are good reasons to expect one or the other. I have no real preference. |
Adding a property to allow defining the |
yes, that's not something for a bugfix |
…aths (xabbuh) This PR was merged into the 3.4 branch. Discussion ---------- [Validator] fix comparisons with null values at property paths | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #29831 | License | MIT | Doc PR | Commits ------- ffa5db7 fix comparisons with null values at property paths
As #29839 has been merged already, I assume this bug can be marked as "fixed". Right? |
Symfony version(s) affected: 4.2.2
Description
Not sure whether this is a bug or the intended behaviour - but at least it seems to be inconsistent with my understanding on how null values are handled in the validator component.
Given two
?\DateTimeImmutable
properties (nullable) in a class to define a timespan whereNULL
reflects unbounded, I'd like to use two comparison constraints to ensure that date 1 is always less than or equal to date 2. TheAbstractComparisonValidator
however only deals withNULL
on the owning side of the constraint and does not handleNULL
on the propertyPath constraint leading to validation errors with theLessThanOrEqual
because the date 1 value (a\DateTimeImmutable
) is not less than or equal toNULL
. TheGreaterThanOrEqual
constraint is no problem - by coincidence - because a\DateTimeImmutable
in date 2 is always greater thanNULL
in date 1.How to reproduce
Possible Solution
Additional context
If required one could add a
NotBlank
orNotNull
to the property to ensure that it must not beNULL
.The text was updated successfully, but these errors were encountered: