symfony/symfony — pull request #8034 by sprain (2013-05-14 10:16:53+00:00)
Title: [Form][Validator] Extended Date Validator with before/after options

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #3640
| License | MIT

Due to personal use I extended the DateValidator to handle 'after' and 'before' options. The existing validation of the value being an actual date is still there.

Example:

// only dates within the year 2014 will be valid.
new Assert\Date(array(
'after' => '2013-12-31'
'before' => '2015-01-01'
)),

It is possible to change the date format in error messages.
In the example below, an error message for the value '2016-01-01' would look like `The date should be before 01.01.2015`

new Assert\Date(array(
'after' => '2013-12-31'
'before' => '2015-01-01'
'message' => 'This value is not a valid date',
'messageAfterDate' => 'The date should be after {{ after }}',
'messageBeforeDate' => 'The date should be before {{ before }}',
'dateFormatMessages' => 'dd.MM.yyyy'
)),

If needed, the DateFormatter can be exchanged completely:

new Assert\Date(array(
'after' => '2013-12-31'
'messageAfterDate' => 'The date should be after {{ after }}',
'dateFormatter' => new \IntlDateFormatter('de_DE', \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE)
)),