trypost/app/Rules/Timezone.php

25 lines
567 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;
class Timezone implements ValidationRule
{
/**
* @param Closure(string, ?string=): 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.');
}
}
}