Skip to content

Commit

Permalink
Validate min and max in date fields (#1801)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianallgeier committed Jul 1, 2019
1 parent f5876cf commit 670c694
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
49 changes: 48 additions & 1 deletion config/fields/date.php
@@ -1,5 +1,7 @@
<?php

use Kirby\Exception\Exception;

return [
'props' => [
/**
Expand Down Expand Up @@ -79,6 +81,51 @@
return '';
},
'validations' => [
'date'
'date',
'minMax' => function ($value) {

$min = $this->min ? strtotime($this->min) : null;
$max = $this->max ? strtotime($this->max) : null;
$value = strtotime($this->value());
$format = 'd.m.Y';
$errors = [];

if ($value && $min && $value < $min) {
$errors['min'] = $min;
}

if ($value && $max && $value > $max) {
$errors['max'] = $max;
}

if (empty($errors) === false) {

if ($min && $max) {
throw new Exception([
'key' => 'validation.date.between',
'data' => [
'min' => date($format, $min),
'max' => date($format, $max)
]
]);
} elseif ($min) {
throw new Exception([
'key' => 'validation.date.after',
'data' => [
'date' => date($format, $min),
]
]);
} else {
throw new Exception([
'key' => 'validation.date.before',
'data' => [
'date' => date($format, $max),
]
]);
}
}

return true;
},
]
];
3 changes: 3 additions & 0 deletions i18n/translations/en.json
Expand Up @@ -183,6 +183,9 @@
"error.validation.contains":
"Please enter a value that contains \"{needle}\"",
"error.validation.date": "Please enter a valid date",
"error.validation.date.after": "Please enter a date after {date}",
"error.validation.date.before": "Please enter a date before {date}",
"error.validation.date.between": "Please enter a date between {min} and {max}",
"error.validation.denied": "Please deny",
"error.validation.different": "The value must not be \"{other}\"",
"error.validation.email": "Please enter a valid email address",
Expand Down
2 changes: 2 additions & 0 deletions panel/src/components/Forms/Input/DateTimeInput.vue
Expand Up @@ -5,6 +5,8 @@
:autofocus="autofocus"
:required="required"
:id="id"
:min="min"
:max="max"
:disabled="disabled"
:value="dateValue"
@input="setDate"
Expand Down

0 comments on commit 670c694

Please sign in to comment.