feat(rules): add rule for CountryCode

This commit is contained in:
Ondrej Vlach 2024-08-07 13:00:31 +02:00
parent 4cef0722cb
commit b0e99f5238
No known key found for this signature in database
GPG Key ID: 7F141CDACEDEE2DE

View File

@ -0,0 +1,28 @@
<?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');
}
}
}