for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Spatie\ValidationRules\Rules;
use Illuminate\Contracts\Validation\Rule;
use League\ISO3166\ISO3166;
class CountryCode implements Rule
{
/** @var bool */
protected $required;
/** @var string */
protected $attribute;
public function __construct(bool $required = true)
$this->required = $required;
}
public function nullable(): self
$this->required = false;
return $this;
public function passes($attribute, $value): bool
$this->attribute = $attribute;
if (! $this->required && ($value === '0' || $value === 0 || $value === null)) {
return true;
$countries = array_pluck((new ISO3166())->all(), ISO3166::KEY_ALPHA2);
return in_array($value, $countries, true);
public function message(): string
return __('validation.country_code', [
'attribute' => $this->attribute,
]);