Skip to content

[Annotation] GreaterThan & DateTime #9390

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
geoffrey-brier opened this issue Oct 28, 2013 · 8 comments
Closed

[Annotation] GreaterThan & DateTime #9390

geoffrey-brier opened this issue Oct 28, 2013 · 8 comments

Comments

@geoffrey-brier
Copy link
Contributor

Hi there,

At work we wanted to check if a date (in a form) was greater than "now". The problem was that we were using annotations in our entity for the validation and (apparently) there is no "simple" way to do so.

The solutions which came to my mind were:

  • Use the "assert greater than" validator directly in the form type ($builder->add('foo', 'bar', array('constraints' => new \DateTime()))
  • Use the annotation @Assert\True on my date field and implement an isDateValid method
  • Create a dedicated annotation
  • Create an @DateTime annotation which would extends from \DateTime and could be used everywhere (in my case : @Assert\GreaterThan(value = @DateTime))

I've chosen to implement the latest of my proposal (@DateTime), it works well (IMO it is flexible, clean and reusable) and I think it could be a very good idea to implement it in symfony.
What do you think about it?
If you feel like it, I can share my work.

@sstok
Copy link
Contributor

sstok commented Oct 28, 2013

There was already a discussion about this.
#7726

@egeloen
Copy link

egeloen commented Oct 28, 2013

@sstok I don't understand why the referenced PR is related to this ticket. According to the discussion, it allows to define an other field in the object graph as reference in a constraint but in this ticket, he doesn't want to compare his field with an other one.

@stof
Copy link
Member

stof commented Oct 28, 2013

actually, it should have been linked to #7766

closing as duplicate

@stof stof closed this as completed Oct 28, 2013
@egeloen
Copy link

egeloen commented Oct 28, 2013

@stof Thanks, anyway, I still don't understand why the referenced PR is related to this ticket. With this addition, if it is merged into the Range constraint, I can't see how I will check if my date is greater than now (with annotation) as I can't provide the min value with new \DateTime('now')

@stof
Copy link
Member

stof commented Oct 28, 2013

@egeloen The PR I linked would support passing "now" as min value. Look at the examples in the description

@egeloen
Copy link

egeloen commented Oct 28, 2013

@stof Okay thx for the clarification

@sstok
Copy link
Contributor

sstok commented Oct 29, 2013

Ah because of the @DateTime I thought you referred to as an property-reference.

@geoffrey-brier
Copy link
Contributor Author

Nop, I was talking about an object.
So now, I know that I have to use the @DateRange validator 👍

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
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

4 participants