选择不显示Laravel vaildation错误的元素

时间:2017-09-29 19:26:28

标签: php laravel laravel-5 laravel-5.3

我没有遇到过这个问题之前我有一个带选项的选择元素,在我的控制器中我把它设置为必需,并且由于某种原因,验证错误没有显示有错误类,甚至没有添加到表单中-group

html和blade

<div class="form-group {{ $errors->has('Test') ? 'has-error' : ''}}">
       <label for="Return_Type">Return Type <span class="request-alert"></span></label>
         <select name="Test" id="Return_Type" class="form-control">
              <option>Choose One...</option>
                  @foreach($categories as $category)
                     <option value="{{ $category->id }}">{{ $category->name }}</option>
                  @endforeach
          </select>
          @if($errors->has('Test'))
             <span class="help-block">
                  {{ $errors->first('Test') }}
             </span>
           @endif
</div>

控制器

 $this->validate($request, [
            'First_Name'    => 'required',
            'Last_Name'     => 'required',
            'Email'         => 'required',
            'Phone'         => 'required',
            'Fax'                        ,
            'Company'                    ,
            'Marketplace'   => 'required',
            'Order_Number'  => 'required',
            'Address_1'     => 'required',
            'Address_2'                  ,
            'City'          => 'required',
            'State'         => 'required',
            'Zip_Code'      => 'required',
            'Test'          => 'required',
            'Product_Name'  => 'required',
            'Quantity'      => 'required',
            'SKU'           => 'required',
        ]);

2 个答案:

答案 0 :(得分:1)

您需要添加==&gt;值=&#34;&#34;在您的第一个选择选项中,即

<option value="">Choose One...</option>

所以将选择表单部分更改为

<div class="form-group {{ $errors->has('Test') ? 'has-error' : ''}}">
   <label for="Return_Type">Return Type <span class="request-alert"></span></label>
     <select name="Test" id="Return_Type" class="form-control">
          <option value="">Choose One...</option>
              @foreach($categories as $category)
                 <option value="{{ $category->id }}">{{ $category->name }}</option>
              @endforeach
      </select>
      @if($errors->has('Test'))
         <span class="help-block">
              {{ $errors->first('Test') }}
         </span>
       @endif

我希望它有所帮助:)

答案 1 :(得分:0)

首先从validate中删除属性fax,c​​ompany和address2,然后添加 在属性Test 'Test' => 'required|exists:Test,id'