Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate select form in laravel

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.

like image 517
Robert Nicjoo Avatar asked Jul 21 '17 09:07

Robert Nicjoo


People also ask

How do you validate exact words in laravel?

To get the exact words to validate you can make use of Rule::in method available with laravel. Using Rule::in method whatever the values provided by this rule has to be matched otherwise it will fail.

How can I validate my name in laravel?

public function store(Request $request) { $data=$request->validate([ // validating the name field. 'name'=>'required']);

What is custom validation in laravel?

You can make the code powerful by using the Laravel custom validation rule with parameters. This streamlines the flow between the logic and code wherever required. Laravel offers various convenient validation rules that can be applied to the data if values are specific to a database table.


1 Answers

This will work perfectly.

$rules = [
    'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];
like image 73
Naresh Kumar Avatar answered Nov 13 '22 18:11

Naresh Kumar