Skip to content

[Validator] Added date support to comparison constraints and Range #11673

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

Merged
merged 1 commit into from
Aug 24, 2014

Conversation

webmozart
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #3640, #7766, #9164, #9390, #8300
License MIT
Doc PR symfony/symfony-docs#4143

This commit adds frequently requested functionality to compare dates. Since the DateTime constructor is very flexible, you can do many fancy things now such as:

/**
 * Only accept requests that start in at least an hour.
 * @Assert\GreaterThanOrEqual("+1 hours")
 */
private $date;

/**
 * Same as before.
 * @Assert\Range(min = "+1 hours")
 */
private $date;

/**
 * Only accept dates in the current year.
 * @Assert\Range(min = "first day of January", max = "first day of January next year")
 */
private $date;

/**
 * Timezones are supported.
 * @Assert\Range(min = "first day of January UTC", max = "first day of January next year UTC")
 */
private $date;

@hacfi
Copy link
Contributor

hacfi commented Aug 15, 2014

Very nice! Thanks @webmozart

@sstok
Copy link
Contributor

sstok commented Aug 15, 2014

👍

@dominikzogg
Copy link
Contributor

n1

@aitboudad
Copy link
Contributor

👍

@weaverryan
Copy link
Member

Don't forget to at least add a docs issue :) - we can just use your examples, they're great!

@webmozart
Copy link
Contributor Author

@weaverryan I'll be a good boy and open a PR there ;)

@Swop
Copy link

Swop commented Aug 17, 2014

👍 Awesome! Thanks @webmozart

@webmozart
Copy link
Contributor Author

I added the docs PR now. Ping @symfony/deciders

@Tobion
Copy link
Contributor

Tobion commented Aug 18, 2014

Maybe add support for DateTimeInterface

// This allows to compare with any date/time value supported by
// the DateTime constructor:
// http://php.net/manual/en/datetime.formats.php
if ($value instanceof \DateTime && is_string($comparedValue)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please support both DateTime and DateTimeInterface (for PHP 5.5+ users using the ImmutableDateTime)

@stof
Copy link
Member

stof commented Aug 18, 2014

Please also add a test validating an ImmutableDateTime rather than a DateTime (running only when the class exists of course)

@webmozart
Copy link
Contributor Author

@stof @Tobion Done.

@stof
Copy link
Member

stof commented Aug 19, 2014

👍

1 similar comment
@Tobion
Copy link
Contributor

Tobion commented Aug 19, 2014

👍

@nicolas-grekas
Copy link
Member

Thank you @webmozart.

@nicolas-grekas nicolas-grekas merged commit 60a5863 into symfony:master Aug 24, 2014
nicolas-grekas added a commit that referenced this pull request Aug 24, 2014
…ts and Range (webmozart)

This PR was merged into the 2.6-dev branch.

Discussion
----------

[Validator] Added date support to comparison constraints and Range

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #3640, #7766, #9164, #9390, #8300
| License       | MIT
| Doc PR        | symfony/symfony-docs#4143

This commit adds frequently requested functionality to compare dates. Since the `DateTime` constructor is very flexible, you can do many fancy things now such as:

```php
/**
 * Only accept requests that start in at least an hour.
 * @Assert\GreaterThanOrEqual("+1 hours")
 */
private $date;

/**
 * Same as before.
 * @Assert\Range(min = "+1 hours")
 */
private $date;

/**
 * Only accept dates in the current year.
 * @Assert\Range(min = "first day of January", max = "first day of January next year")
 */
private $date;

/**
 * Timezones are supported.
 * @Assert\Range(min = "first day of January UTC", max = "first day of January next year UTC")
 */
private $date;
```

Commits
-------

60a5863 [Validator] Added date support to comparison constraints and Range
@lmammino
Copy link

Wow! Great addition 😉

weaverryan added a commit to symfony/symfony-docs that referenced this pull request Jun 28, 2015
…mparison constraints and Range (webmozart, javiereguiluz)

This PR was submitted for the 2.7 branch but it was merged into the 2.6 branch instead (closes #5384).

Discussion
----------

Added information about the new date handling in the comparison constraints and Range

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes (symfony/symfony#11673)
| Applies to    | 2.6+
| Fixed tickets | -

This PR finishes #4143

Commits
-------

b6c1a93 Added the "payload" option back
7ef2e6a Show annotations first
e3efbbf Reordered the code blocks to show Annotations, YAML, XML and PHP
39f46e1 Fixed the issues reported by @xabbuh
7003445 Finished the documentation of the new data comparison validators
1fa69fe Added information about the new date handling in the comparison constraints and Range
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.