Skip to content
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

[2.3] [Validator] added comparison constraints and validators #790

Merged
merged 1 commit into from
Apr 30, 2013
Merged

[2.3] [Validator] added comparison constraints and validators #790

merged 1 commit into from
Apr 30, 2013

Conversation

danielholmes
Copy link
Contributor

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
/**
 * @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
/** @assert:GreaterThan(80) */
private $iq;
/** @assert:LessThan('dateDied') */
private $bornDate;
/** @assert:LessThanOrEqual('C') */
private $grade;
/** @assert:GreaterThanOrEqual({50, 'ageStartedSchool'}) */
private $age;

@ghost ghost assigned webmozart May 6, 2011
$values[] = $object->$getMethod();
} else {
$error = sprintf('No method "%s" on object of type "%s"', $getMethod,
get_class($object));
Copy link
Member

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

@stof
Copy link
Member

stof commented Sep 4, 2011

all the constraint classes should now be tagged with @Annotation to make them usable as annotations.

@danielholmes could you update the PR ?

{
$valid = false;

if ($object === null) {
Copy link
Member

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

@stof
Copy link
Member

stof commented Nov 11, 2011

@danielholmes ping. could you update the PR according to the comments ?

@danielholmes
Copy link
Contributor Author

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();
Copy link
Member

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 ?

Copy link
Contributor Author

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

@raziel057
Copy link
Contributor

It would be cool to integrate this PR.

@stof
Copy link
Member

stof commented Apr 3, 2012

@danielholmes could you rebase your PR as tests have been moved to a different location ?

@bschussek can you review it ?

@danielholmes
Copy link
Contributor Author

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

@stof
Copy link
Member

stof commented Apr 4, 2012

@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

@danielholmes
Copy link
Contributor Author

Amazing! Thanks so much for the tip @stof , that did the trick

$values[] = $reflProperty->getValue($object);
$valueFound = true;
} else {
foreach ($getterMethods as $getterMethod) {
Copy link
Contributor

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 ?

Copy link
Contributor

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;

Copy link
Contributor Author

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

@vicb
Copy link
Contributor

vicb commented Jul 3, 2012

@danielholmes do you have time to update the PR according to the comments ?

@danielholmes
Copy link
Contributor Author

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

@vicb
Copy link
Contributor

vicb commented Jul 3, 2012

@danielholmes that will be ok. Thanks for your help.

}
}

return $valid;
Copy link
Member

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

Copy link
Contributor Author

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

@danielholmes
Copy link
Contributor Author

@bschussek @stof looks like 2.3 is stabilising, will this be pushed back to 2.4+ then?

@fabpot
Copy link
Member

fabpot commented Apr 29, 2013

I would love to have this in 2.3 as this is now the oldest PR for Symfony.

*/
public function __construct(PropertyPathInterface $path = null)
Copy link
Contributor

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.

Copy link
Contributor

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.

@webmozart
Copy link
Contributor

@danielholmes Since no conclusion has been made on the reference handling yet, we have two ways to go now:

  • Remove reference handling from this PR and merge it without
  • Wait for 2.4

What do you prefer? On a side note, feature freeze is tomorrow.

@danielholmes
Copy link
Contributor Author

@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

@danielholmes
Copy link
Contributor Author

@bschussek removed property references so it only deals with values now

@marcospassos
Copy link

@danielholmes Will you work on Reference PR? I really need this, so if you need some help, please, let me know.

@danielholmes
Copy link
Contributor Author

@marcospassos I don't really understand the Reference PR (actually I don't think there's a PR atm, just an issue to decide on design decisions: #7726 ). I'm more than happy to implement it, but there doesn't seem to be a consensus about what should be implemented

*
* @return Boolean true if the relationship is valid, false otherwise
*/
abstract protected function compareValues($value1, $value2, Constraint $constraint);
Copy link
Contributor

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?

@danielholmes
Copy link
Contributor Author

@bschussek Feedback addressed

@webmozart
Copy link
Contributor

👍

@webmozart
Copy link
Contributor

Could you please squash the commits? There's a lot of changes in here that clutter up the history.

@danielholmes
Copy link
Contributor Author

@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

@webmozart
Copy link
Contributor

Splendid, thank you! :)

fabpot added a commit that referenced this pull request Apr 30, 2013
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.
@fabpot fabpot merged commit 0bffdff into symfony:master Apr 30, 2013
@fabpot
Copy link
Member

fabpot commented Apr 30, 2013

@danielholmes Merge now, thanks! Can you open a PR on the documentation repository?

stloyd added a commit to stloyd/symfony that referenced this pull request May 2, 2013
wouterj added a commit to wouterj/symfony that referenced this pull request May 2, 2013
fabpot added a commit that referenced this pull request May 3, 2013
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
fabpot added a commit that referenced this pull request May 3, 2013
This PR was merged into the master branch.

Discussion
----------

[Validation] Add dutch translation for new validators

This PR already uses the dots added in #7911

Commits
-------

84a0618 Add translation for new validators from #790
fabpot added a commit that referenced this pull request May 8, 2013
This PR was merged into the master branch.

Discussion
----------

[Validator] Basque translations for new validators from #790

Commits
-------

971f551 Basque translations for new validators from #790
@raziel057
Copy link
Contributor

Any chance to discuss about the Reference validator to include it in Symfony 2.5 ? It's a important and interesting feature.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants