Ravindra24.com

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>
					  <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>
								
						  </select>
						  <span class="text-danger"> @error('country') {{$message}} @enderror </span>
          			</div>
					  <div class="form-section">
          				<span class="fa fa-map-marker-alt input-icon"></span>
          				<select name="category">{{old('category')}}
							  <option>Select category</option>
							  
							  <option>Business</option>
							  <option>Digital Services</option>
							  <option>Business & Services</option>
							  <option>Education</option>
							  <option>Entertainment</option>
							  <option>News & Politics</option>
							  <option>People & Blogs</option>
							  <option>Tour and Travels</option>
							  <option>Realestate</option>
							  <option>Technologies</option>
							  <option>Other</option>
						  </select>
						  <span class="text-danger"> @error('category') {{$message}} @enderror </span>
          			</div>

Controller file


$request->validate([
            
            'country'=>'required',
            'category'=>'required',
            
        ]);

Answer

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.

$request->validate([
            
            'country'=>'required|not_in:Select country',
            'category'=>'required|not_in:Select category',
            
        ]);

Leave a Comment