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

Vusumzi's avatar

How to display an error message of a select field in laravel

I was trying to validate a form, but it seems that I cant show the error message for the dropdown field.

here is my updade.blade.php

<div class="form-group">
<select id="province" name="province" class="form-control @error('province') is-invalid @enderror" >
                                        <option>Choose...</option>
                                        <option value="Western Cape" {{ ($user->learner->province == "Western Cape") ? 'selected' : ''}}>Western Cape</option>
                                        <option value="Northern Cape" {{ ($user->learner->province == "Northern Cape") ? 'selected' : '' }}>Northern Cape</option>
                                        <option value="Eastern Cape" {{ ($user->learner->province == "Eastern Cape") ? 'selected' : '' }}>Eastern Cape</option>
                                        <option value="North West" {{ ($user->learner->province == "North West") ? 'selected' : '' }}>North West</option>
                                        <option value="Free State" {{ ($user->learner->province == "Free State") ? 'selected' : '' }}> Free State</option>
                                        <option value="Kwazulu Natal" {{ ($user->learner->province == "Kwazulu Natal") ? 'selected' : '' }}>Kwazulu Natal</option>
                                        <option value="Gauteng" {{ ($user->learner->province == "Gauteng") ? 'selected' : '' }}>Gauteng</option>
                                        <option value="Limpopo" {{ ($user->learner->province == "Limpopo") ? 'selected' : '' }}>Limpopo</option>
                                        <option value="Mpumalanga" {{ ($user->learner->province == "Mpumalanga") ? 'selected' : '' }}>Mpumalanga</option>
                                    </select>
                                    @error('province')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>

and here is the update method from the UserController

public function update(Request $request, $id)
    {
        $this->validate($request,[
            'email' => 'email',
            'phone' => 'min:10|max:15',
            'gender' => 'required',
            'identity' => 'required',
            'password' => 'string|min:8|confirmed',
            'street' => 'required',
            'suburb' => 'required',
            'city' => 'required',            
            'province' => 'required|not_in:0',
            'code' => 'required',
            'school' => 'required',
            'grade' => 'required|not_in:0'
        ]);
    
        $user = User::find($id);
        $user->update($request->all());

           if($user->types == 'Learner'){
            $learner = Learner::where('user_id',$id)->first();
            $learner->gender = $request->input('gender');
            $learner->identity = $request->input('identity');
            $learner->street = $request->input('street');
            $learner->suburb = $request->input('suburb');
            $learner->city = $request->input('city');
            $learner->province = $request->input('province');
            $learner->code = $request->input('code');
            $learner->school = $request->input('school');
            $learner->grade = $request->input('grade');
            $learner->save();
            return redirect()->back()->with('message','User Updated Successfully');
           }
}

I want the error message to be displayed at the bottom of the select field and the field to have a red border around... Its working fine for other input fields but the problem is the select field...

0 likes
7 replies
jeevamugunthan's avatar

@vusumzi make this changes in your code

<option  value="">Choose...</option>

and in your controller

'province' => 'required',

and also check your header form data in network tab, for any values is there in the name of province

1 like
a4ashraf's avatar

@jeevamugunthan

see the documentation for not_in validation, apply it correctly

not_in:foo,bar,... The field under validation must not be included in the given list of values. The Rule::notIn method may be used to fluently construct the rule:


use Illuminate\Validation\Rule;

Validator::make($data, [
    'toppings' => [
        'required',
        Rule::notIn(['sprinkles', 'cherries']),
    ],
]);
1 like
a4ashraf's avatar

@jeevamugunthan

If you need to retrieve an array of all the messages for a given field, use the get method:

foreach ($errors->get('province') as $message) {
    <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
}
1 like
Vusumzi's avatar

Very Good... Thanks

Now How do you store Checkbox values in a database Table? and how do you retrieve them if you want to update certain values?

Vusumzi's avatar

Very Good... Thanks

Now How do you store Checkbox values in a database Table? and how do you retrieve them if you want to update certain values?

tarikmanoar's avatar

Your province validation is failing. You're asking Laravel to validate that your province is not the value 0. If you don't choose an option, the value of your select element is actually 'Choose...' which is not 0 and therefore not failing.

If you set the Choose... option value to be value="0" your rule will pass and you'll see an error message.

<option value="0">Choose...</option>

Please or to participate in this conversation.