Skip to content

[2.2] [Validator] After and Before Date In Validator #3640

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
patrick-mcdougle opened this issue Mar 19, 2012 · 4 comments
Closed

[2.2] [Validator] After and Before Date In Validator #3640

patrick-mcdougle opened this issue Mar 19, 2012 · 4 comments

Comments

@patrick-mcdougle
Copy link
Contributor

Enhancement: Extend the Date and DateTime validators to have options for before and after. It would be awesome if you could do

/*
* @Assert\Date(before = "9/31/2015", after = 'today')
*/
@patrick-mcdougle
Copy link
Contributor Author

Does anyone else have any thoughts about this? @fabpot?

@sstok
Copy link
Contributor

sstok commented Mar 22, 2012

Doing this is not difficult as the DateTime extension also supports the strtotime() format.

One suggestion (this is something I'm using my self in an [WIP] bundle).
In some cases you need to use dynamic values, and hardcoded values are problematic then.

Calling an function for getting the value will fix this, and when using the DIC, this can be as easy as calling a service inside a View. So the user can either set a static value, or a Service callback like. %service_id%:funcname

Parameters for the function can be given by starting them with an underscore and then and resolving them to the parameter-name (like Controller parameters are resolved).

Example.

/*
* @Assert\Date(before = "9/31/2015", after = '%service_id%:funcname', _name1=true)
*/

@briancappello
Copy link

A custom validator can do the trick.
See http://blog.michaelperrin.fr/2013/03/19/range-date-validator-for-symfony2/ for an example.

nicolas-grekas added a commit that referenced this issue 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
@javiereguiluz
Copy link
Member

I think that we should close this issue because it's been fixed by #11673.

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

No branches or pull requests

5 participants