ogsoft-example/app/Rules/CountryCodeExists.php

29 lines
683 B
PHP

<?php
declare(strict_types=1);
namespace App\Rules;
use App\Models\Country;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Determinate if given country code exists (only CZ for now, for example, add more countries as needed)
*/
class CountryCodeExists implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!in_array($value, ['CZ'])) {
$fail('validation.country_code_exists');
}
$country = Country::where('country_code', $value)->first();
if ($country === null) {
$fail('validation.country_code_exists_in_db');
}
}
}