验证 Laravel 中的选择表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45234213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:20:55  来源:igfitidea点击:

Validate select form in laravel

phplaravelvalidation

提问by Robert Nicjoo

I have this html on my contact form:

我的联系表格上有这个 html:

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

谢谢。

回答by R?sh?Kêsh Kümar

First Give THe Name To Selectdrop down fields, then after Simplest Way Is use in Validation Rules with the name of selectinput fields. that you give required

首先给出Select下拉字段的名称,然后在最简单的方法之后在验证规则中使用select输入字段的名称。你给的required

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',
    ...
];

回答by Maraboc

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:0to avoid submission of the first choice witch is --Select type--:)

not_in:0避免提交的首选女巫是--Select type--:)

And to pass infos to the validation just use $request->all()because it will use the field namein this case typeSelectionno need for ctreating a new array !

并将信息传递给验证只需使用,$request->all()因为name在这种情况下它将使用该字段而typeSelection无需处理新数组!

回答by Naresh Kumar

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

回答by AddWeb Solution Pvt Ltd

You need to validate your select with requiredand for that you need to setup select like:

您需要验证您的选择,required为此您需要设置选择,例如:

Select into blade:

选择刀片:

<select class="form-control" id="type" name="selType">
    <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 somewhere:

某处的验证规则:

$rules = [
    'selType' => 'required'
];