mirror of
https://github.com/Respect/Validation.git
synced 2026-03-15 23:05:45 +01:00
Since I updated the validation engine[1], it became possible to create
results with siblings. This commit changes the "DateTimeDiff", allowing
it to create a result with a sibling when possible. That will improve
the clarity of the error messages.
While at it, I noticed that we were not translating the type of
interval, so I fixed that and improved the documentation.
[1]: 238f2d506a
4 KiB
4 KiB
DateTimeDiff
DateTimeDiff(string $type, Rule $rule)DateTimeDiff(string $type, Rule $rule, string $format)DateTimeDiff(string $type, Rule $rule, string $format, DateTimeImmutable $now)
Validates the difference of date/time against a specific rule.
The $format argument should follow PHP's date() function. When the $format is not given, this rule accepts
Supported Date and Time Formats by PHP (see strtotime()).
v::dateTimeDiff('years', v::equals(7))->isValid('7 years ago'); // true
v::dateTimeDiff('years', v::equals(7))->isValid('7 years ago + 1 minute'); // false
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->isValid('09/12/1990'); // true
v::dateTimeDiff('years', v::greaterThan(18), 'd/m/Y')->isValid('09/12/2023'); // false
v::dateTimeDiff('months', v::between(1, 18))->isValid('5 months ago'); // true
The supported types are:
yearsmonthsdayshoursminutessecondsmicroseconds
Templates
The first two templates serve as message suffixes:
v::dateTimeDiff('years', v::equals(2))->assert('1 year ago')
// The number of years between now and 1 year ago must be equal to 2
v::not(v::dateTimeDiff('years', v::lessThan(8)))->assert('7 year ago')
// The number of years between now and 7 year ago must not be less than 8
DateTimeDiff::TEMPLATE_STANDARD
Used when $format and $now are not defined.
| Mode | Template |
|---|---|
default |
The number of {{type|trans}} between now and |
inverted |
The number of {{type|trans}} between now and |
DateTimeDiff::TEMPLATE_CUSTOMIZED
Used when $format or $now are defined.
| Mode | Template |
|---|---|
default |
The number of {{type|trans}} between {{now|raw}} and |
inverted |
The number of {{type|trans}} between {{now|raw}} and |
DateTimeDiff::TEMPLATE_WRONG_FORMAT
Used when the input cannot be parsed with the given format.
| Mode | Template |
|---|---|
default |
For comparison with {{now|raw}}, {{name}} must be a valid datetime in the format {{sample|raw}} |
inverted |
For comparison with {{now|raw}}, {{name}} must not be a valid datetime in the format {{sample|raw}} |
Template placeholders
| Placeholder | Description |
|---|---|
name |
The validated input or the custom validator name (if specified). |
now |
The date and time that is considered as now. |
sample |
A sample of the datetime. |
type |
The type of interval (years, months, etc.). |
Caveats
When using custom templates, the key must be dateTimeDiff + name of the rule you passed, for example:
v::dateTimeDiff('years', v::equals(2))->assert('1 year ago', [
'dateTimeDiffEquals' => 'Please enter a date that is 2 years ago'
]);
// Please enter a date that is 2 years ago.
Categorization
- Date and Time
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Created from Age, MinAge, and MaxAge |
See also: