feat(rules): add rule for CountryCode
This commit is contained in:
parent
4cef0722cb
commit
b0e99f5238
28
app/Rules/CountryCodeExists.php
Normal file
28
app/Rules/CountryCodeExists.php
Normal 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');
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user