diff --git a/config/fields/date.php b/config/fields/date.php index 08a0902462..cf518c2357 100644 --- a/config/fields/date.php +++ b/config/fields/date.php @@ -1,5 +1,7 @@ [ /** @@ -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; + }, ] ]; diff --git a/i18n/translations/en.json b/i18n/translations/en.json index a48bb07c86..1f5ae1a112 100644 --- a/i18n/translations/en.json +++ b/i18n/translations/en.json @@ -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", diff --git a/panel/src/components/Forms/Input/DateTimeInput.vue b/panel/src/components/Forms/Input/DateTimeInput.vue index 1eed4e2690..d0c38581e7 100644 --- a/panel/src/components/Forms/Input/DateTimeInput.vue +++ b/panel/src/components/Forms/Input/DateTimeInput.vue @@ -5,6 +5,8 @@ :autofocus="autofocus" :required="required" :id="id" + :min="min" + :max="max" :disabled="disabled" :value="dateValue" @input="setDate"