Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

morariu's avatar

Making the country_id drop-down field as a required field?

Perhaps a silly question but how do I validate and make drop-down field required? Such as to make country selection mandatory.

Adding the field required in a RegisterRequest.php does not seem to do it.

Thank you.

0 likes
3 replies
Cronix's avatar

required works, if your default selected option has value="", so if they don't choose a country it will submit an empty string, which will fail the required rule.

<option value="" selected>Choose your country</option>
...
<option value="usa">United States</option>
<option value="can">Canada</option>
...
etc
1 like
morariu's avatar

@Cronix thank you, in my controller I have the following:

    public function getRegister(CountryRepository $countryRepository)
    {
        
        //$countries = $countryRepository->lists();
        $countries = [0 => 'Select a Country'] + $countryRepository->lists()->toArray();
        return view('auth.register', compact('socialProviders', 'countries'));
    }

Please or to participate in this conversation.