Validate select form in laravel

Viewed 41126

I have this html on my contact form:

<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">
                  <option>Business</option>
                  <option>Company</option>
                  <option>Personal</option>
                  <option>Online Shopping</option>
                  <option>Other</option>
               </select>
          </div>
     </div>
</div>

How can I validate and pass data of this field in email?

Currently I have this:

<!-- Text input-->
<div class="form-group">
      <label class="col-md-2 control-label">Type</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>
               <input  name="type" placeholder="eg. Business, Company, Personal, Online Shopping, etc." class="form-control"  type="text">
           </div>
      </div>
</div>

And I validate and pass it like this:

$data = array(
        'type' => $request->type,
      );

but that's input field what I want is my first sample (select box), so I'm not sure how to do it!

thanks.

7 Answers

This will work perfectly.

$rules = [
    'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];

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

if you want to check if the selected value is exist in your table's column you can check it like this 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>

for above selection

'Business_Type' => 'required|my_business ,type'

This will make sure the selected value exists for example in the my_business table on the type column. if you select the first option it will return the error

The selected type is invalid.


You should put Value in each option The first one should be

value=""

and the rest put the values of them like this

   <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>
Related