Validate select form in laravel
You can do it like this :
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="typeSelection">
<option>--Select type--</option>
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
For the rules :
$rules = [
'typeSelection' => 'required|not_in:0'
];
not_in:0
to avoid submission of the first choice witch is --Select type--
:)
And to pass infos to the validation just use $request->all()
because it will use the field name
in this case typeSelection
no need for ctreating a new array !
First Give THe Name To
Select
drop down fields, then after Simplest Way Is use in Validation Rules with the name ofselect
input fields. that you giverequired
Example:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules
// Validation rules somewhere...
$rules = [
...
'Business_Type' => 'required',
...
];
In Laravel 8 i use this, i found out that if you change the value to an other value trough inspect element 'yes i know this sounds funky' still can be inserted into the validator and pass. Here's the solution
Select:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option></select>
Validator:
$this->validate($request, [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
]);
Just enter the values that the validator must pass, all other values will not pass
This will work perfectly.
$rules = [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];