There is a rule for checking if the parameter exists in an array of values
https://laravel.com/docs/5.8/validation#rule-in
but this would be a long rule
or if you have the countries in a database table, then use the exists rule
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, i am trying to build a simple registration form which includes a select with country list like this
<select id="country" class="form-control" name="country" required>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
<option value="Andorra">Andorra</option>
<option value="Angola">Angola</option>
<option value="Anguilla">Anguilla</option>
<option value="Antartica">Antarctica</option>
<option value="Antigua and Barbuda">Antigua and Barbuda</option>
<option value="Argentina">Argentina</option>
<option value="Armenia">Armenia</option>
<option value="Aruba">Aruba</option>
...
I can't find a way to validate this in User model, i tried to inject something else as country and it works.
here's my registration controller validation part
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'lname' => ['required', 'string', 'max:255'],
'country' => ['required', 'string', 'max:45'],
'address' => ['required', 'string', 'max:100'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
I was also thinking about putting all these countries inside one array and try to compare it to the data received in controller like so, but have no idea where to put it
$countries = ['us', 'am', 'ru' .... others]
$country = $data['country'];
if(!in_array($country, $countries){
return false
}
There is a rule for checking if the parameter exists in an array of values
https://laravel.com/docs/5.8/validation#rule-in
but this would be a long rule
or if you have the countries in a database table, then use the exists rule
Please or to participate in this conversation.