ogsoft-example/app/Rules/DateYMD.php

25 lines
570 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Validate a date in the format YYYY-MM-DD.
*/
class DateYMD implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
$fail('validation.format.date_ymd');
}
if (\DateTimeImmutable::createFromFormat("Y-m-d", $value) === false) {
$fail('validation.format.date_ymd');
}
}
}