Skip to content

[Validator] DateRange constraint added #7766

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

Closed
wants to merge 1 commit into from

Conversation

HadesArchitect
Copy link
Contributor

Added DateRange constraint and appropriate translations in russian and english.

Simple example:

    /**
     * @Assert\DateRange(
     *      min    = "01/01/1930",
     *      max    = "01/01/2012",
     * )
     */
    protected $birthday;

Custom format example:

Date format by default is d/m/Y, but easily can be overridden by 'format' property of constraint, f.e.:

    /**
     * @Assert\DateRange(
     *      min    = "01.01.1930",
     *      max    = "01.01.2012",
     *      format = "d.m.Y"
     * )
     */
    protected $birthday;

This is my first commit to symfony components so feel free to request any additionals - write docs, improve something and so on.

@cursedcoder
Copy link

I would like to add relative formats supported by DateTime

http://php.net/manual/en/datetime.formats.relative.php

@HadesArchitect
Copy link
Contributor Author

@cursedcoder It's a good idea, i think.

@webmozart
Copy link
Contributor

I think this should be merged into Range. We don't need the format option, it's sufficient if you support the standard format yyyy-mm-dd.

Apart from that, what about timezones? Can you rely on the server's timezone for comparison? What if $birthday contains the date "2012-01-01 GMT-01:00", this would be equivalent to "2012-01-01 01:00 GMT" and consequently fail your constraint because it is greater than "2012-01-01 00:00".

Good idea in general, but this needs a lot of thought :)

<target>This date should be {{ limit }} or earlier.</target>
</trans-unit>
<trans-unit id="66">
<source>This value should be {{ limit }} or later.</source>
Copy link
Contributor

Choose a reason for hiding this comment

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

value -> date

Copy link
Member

Choose a reason for hiding this comment

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

The other way round: date should be value above.

@bes89
Copy link
Contributor

bes89 commented Sep 20, 2013

I solved this problem by using the comparison constraints:

                            new Assert\Date(),
                            new Assert\GreaterThanOrEqual(array(
                                'value' => new \DateTime()
                            )),
                            new Assert\LessThanOrEqual(array('value' => new \DateTime(
                                date('Y-m-d', strtotime("+2 month"))
                            ))),

But it has a disadvantage:
Assert\Date is accepting "strings" - that's okay but if we use the comparison constraints with date input we have to ensure that a DateTime object is given because it's comparable and strings are not. I used a form ViewTransformer to solve this.

as @bschussek said above it should be merged into Range as we don't need a DateRange

@hacfi
Copy link
Contributor

hacfi commented Sep 21, 2013

@Exelenz Very nice idea - look forward to using this on my current project!

@sstok
Copy link
Contributor

sstok commented Sep 22, 2013

Related to #7726 and we now have the ExpressionLanguage Component making the original idea allot more powerful

@diimpp
Copy link
Member

diimpp commented Jul 28, 2014

So what happened in result? There is number of PR for this functionality. Did anyone get merged?

@webmozart
Copy link
Contributor

Replaced by #11673.

@webmozart webmozart closed this Aug 15, 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
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.

9 participants