How do I validate a drop down list in Laravel PHP?

I am working on a project where I need to validate a dropdown lost which coded as select tag of HTML so I want to validate it with blank value so that user may not leave it blank.so following is my Laravel blade file and controller code.

Blade View File

					  <div class="form-section">
          				<span class="fa fa-map-marker-alt input-icon"></span>
          				<select name="country">
							  <option value="Select country">Select country</option>
							  <option value="Afganistan">Afghanistan</option>
								<option value="Albania">Albania</option>
								<option value="Algeria">Algeria</option>
								<option value="American Samoa">American Samoa</option>
						  <span class="text-danger"> @error('country') {{$message}} @enderror </span>
					  <div class="form-section">
          				<span class="fa fa-map-marker-alt input-icon"></span>
          				<select name="category">{{old('category')}}
							  <option>Select category</option>
							  <option>Digital Services</option>
							  <option>Business & Services</option>
							  <option>News & Politics</option>
							  <option>People & Blogs</option>
							  <option>Tour and Travels</option>
						  <span class="text-danger"> @error('category') {{$message}} @enderror </span>

Controller file



In the controller file you have mentioned validation in wrong way, You can use required|not_in:Select country to validate the select in blade file and following is the code of controller file so follow the given syntax to make it working.

            'country'=>'required|not_in:Select country',
            'category'=>'required|not_in:Select category',

Leave a Comment