trypost/app/Rules/Timezone.php

24 lines
536 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class Timezone implements ValidationRule
{
/**
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
try {
new \DateTimeZone($value);
} catch (\Exception) {
$fail('The :attribute must be a valid timezone.');
}
}
}