-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.3] [Validator] added comparison constraints and validators #790
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
Conversation
$values[] = $object->$getMethod(); | ||
} else { | ||
$error = sprintf('No method "%s" on object of type "%s"', $getMethod, | ||
get_class($object)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should stay on the previous line
all the constraint classes should now be tagged with @danielholmes could you update the PR ? |
{ | ||
$valid = false; | ||
|
||
if ($object === null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be null === $object
according to the CS
@danielholmes ping. could you update the PR according to the comments ? |
Hi @stof, apologies for the late reply on this one. Thanks for all the feedback, I've addressed it all now. Let me know if there's anything else. |
* | ||
* @return string The human readable symbol that represents the comparison | ||
*/ | ||
abstract public function getSymbol(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does it really need to be public ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No that was just laziness - it was just making the tests simpler. I've made it public now
It would be cool to integrate this PR. |
@danielholmes could you rebase your PR as tests have been moved to a different location ? @bschussek can you review it ? |
I've rebased, moved the tests to the new locations and fixed them up to work with current code. Unfortunately the rebase looks a bit messy - not sure what I did wrong |
@danielholmes seems like you merged your older remote branch after rebasing instead of forcing the push. this means you duplicated the history instead of cleaning it. Here is the way to clean things: # create a temp branch as backup
git branch tmp
# reset your branch before the wrong merge commit
git reset --hard 677559d
# cherry-pick the commit done after the merge to move the tests
git cherry-pick 3da62d2
# force the push to github to overwrite the old (messy) branch
# /!\ take care to specify the branch name to be sure not to mess other branches in your github repo
git push origin comparison_validators --force
# eventually delete the backup branch once you checked on github that it went fine
git branch -D tmp |
Amazing! Thanks so much for the tip @stof , that did the trick |
$values[] = $reflProperty->getValue($object); | ||
$valueFound = true; | ||
} else { | ||
foreach ($getterMethods as $getterMethod) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't getters be checked first as they should be the preferred way to access the propery ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it could be:
<?php
if (getter) {
$values[] = ...;
continue;
}
if (property) {
$values[] = ...;
continue;
}
throw new;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just made a commit to fix this, i.e. favour getters over properties
@danielholmes do you have time to update the PR according to the comments ? |
Right now is pretty bad timing for me. I'm flying to the US in 4 days so have a pretty tight schedule until then tying things off. I'll have time to look at it in about 7 days - hopefully that fits in okay with the release schedule |
@danielholmes that will be ok. Thanks for your help. |
} | ||
} | ||
|
||
return $valid; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the return value should be removed once you switch to validate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a commit to update this
@bschussek @stof looks like 2.3 is stabilising, will this be pushed back to 2.4+ then? |
I would love to have this in 2.3 as this is now the oldest PR for Symfony. |
*/ | ||
public function __construct(PropertyPathInterface $path = null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is BC (between 2.2 & 2.3) and should be noted in CHANGELOG file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't see where this breaks BC.
@danielholmes Since no conclusion has been made on the reference handling yet, we have two ways to go now:
What do you prefer? On a side note, feature freeze is tomorrow. |
@bschussek my preference then would be to merge it in with values only. References are really useful (start and end dates, from and to prices, etc), but obviously better if it's done right. I'll work on removing references now and have an updated PR ready soon, unless there's any objections |
@bschussek removed property references so it only deals with values now |
@danielholmes Will you work on |
@marcospassos I don't really understand the |
* | ||
* @return Boolean true if the relationship is valid, false otherwise | ||
*/ | ||
abstract protected function compareValues($value1, $value2, Constraint $constraint); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove the $constraint parameter?
@bschussek Feedback addressed |
👍 |
Could you please squash the commits? There's a lot of changes in here that clutter up the history. |
@bschussek done. The property access changes we made along the way were now unrelated to the validators, so I moved them to their own PR if you still think they'd be useful: #7876 |
Splendid, thank you! :) |
This PR was merged into the master branch. Discussion ---------- [2.3] [Validator] added comparison constraints and validators | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | They work on a class level and you specify a list of properties. Included are the standard GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual and Equal. e.g.: ```php <?php /** * @Assert:GreaterThan({"diedDate", "bornDate"}) * @Assert:LessThanOrEqual({"bornDate", "firstSchoolDayDate", "diedDate"}) */ class Person { private $bornDate; private $firstSchoolDayDate; private $diedDate; } ``` I think it would also be useful if they worked on a property level rather than just class. I'm not sure what the default option should be though. e.g. is there a reliable way to determine what's a raw value and what's a property name: ```php <?php /** @Assert:GreaterThan(80) */ private $iq; /** @Assert:LessThan('dateDied') */ private $bornDate; /** @Assert:LessThanOrEqual('C') */ private $grade; /** @Assert:GreaterThanOrEqual({50, 'ageStartedSchool'}) */ private $age; ``` Commits ------- 0bffdff [Validator] Added comparison validators.
@danielholmes Merge now, thanks! Can you open a PR on the documentation repository? |
This PR was merged into the master branch. Discussion ---------- [Validator] Add missing translation for new validators from #790 > Note: dots are added to be consistent with other validators. | Q | A | ------------- | --- | Bug fix? | yes | License | MIT Commits ------- 90a7e86 [Validator] Add missing translation for new validators from #790
Any chance to discuss about the |
They work on a class level and you specify a list of properties. Included are the standard GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual and Equal. e.g.:
I think it would also be useful if they worked on a property level rather than just class. I'm not sure what the default option should be though. e.g. is there a reliable way to determine what's a raw value and what's a property name: