0字段是必需的。 Laravel 5.5验证错误

时间:2018-06-12 07:12:23

标签: php mysql laravel

我有一个"需要0字段。"当我没有任何字段调用0时尝试将数据保存到数据库时出错。如果没有来自Controller的验证,数据会保存,但如果我甚至只验证了我要验证的六个字段中的一个字段,我仍然会收到错误。我该如何解决这个问题。请帮忙,这是我的观点

<form method="post" action="{{ url('agent/add_tenantProperty') }}" data-toggle="validator">
                {{ csrf_field() }}
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="txtMovieTitle">Tenant</label>
                     <select id="ddlGenge" class="form-control" name="tenant_id" required="">
                       @foreach($tenants as $tenant)
                        <option value="{{ $tenant->id }}">
                          {{ $tenant->designation }} {{ $tenant->firstname }} {{ $tenant->lastname }}
                        </option>
                       @endforeach
                      </select>
                       <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="ddlGenge">Asset Category</label>
                      <select id="ddlGenge" class="form-control" name="asset_id" required="">
                        <option>Choose a Property</option>
                        @foreach($assets as $asset)
                         <option value="{{ $asset->id }}">{{ $asset->category }}</option>
                        @endforeach
                      </select>
                       <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="txtDirector">Asset description</label>
                      <select id="ddlGenge" class="form-control" name="description" required="">
                        <option>Choose a Description</option>
                        @foreach($assets as $asset)
                          <option value="{{ $asset->description }}">{{ $asset->description }}</option>
                        @endforeach
                      </select>
                    <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="txtProducer">Location</label>
                      <select id="ddlGenge" class="form-control" name="address" required="">
                        <option>Choose an Address</option>
                        @foreach($assets as $asset)
                          <option value="{{ $asset->address }}">{{ $asset->address }}</option>
                        @endforeach
                      </select>  
                    <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="txtWebsite">Standard price</label>
                      <input id="txtWebsite" type="text" class="form-control" name="price" required="">
                      <div class="help-block with-errors"></div>
                    </div>
                  </div>
                  <div class="col-md-6">
                    <div class="form-group">
                      <label for="txtWriter">Date</label>
                      <input id="txtWriter" type="date" class="datepicker form-control" name="occupation_date"
                             required="">
                    <div class="help-block with-errors"></div>
                    </div>
                  </div>
                </div>
                <button type="submit" class="btn btn-outline btn-primary pull-right">Submit</button>
                <br/>
              </form>

和我的控制器

public function store(Request $request)
{
    //validation
    $this->validate($request, array([
        'tenant_id' => 'required',
        'asset_id' => 'required',
        'description' => 'required',
        'address' => 'required',
        'price' => 'required',
        'occupation_date' => 'required',
    ]));

    //create and save new data
    $tenantProperty = New TenantProperty();
    $tenantProperty->tenant_id = $request->tenant_id;
    $tenantProperty->asset_id = $request->asset_id;
    $tenantProperty->description = $request->description;
    $tenantProperty->address = $request->address;
    $tenantProperty->price = $request->price;
    $tenantProperty->occupation_date = $request->occupation_date;
    $tenantProperty->save();
    //redirect
    return redirect('agent/tenantProperty_list');
}

路线如下

 Route::get('add_tenantProperty', 'TenantPropertyController@create')->name('/add_tenantProperty');
Route::post('add_tenantProperty', 'TenantPropertyController@store');

3 个答案:

答案 0 :(得分:3)

当您只编写$request时,它会传递整个请求对象,但验证函数希望这两个参数都是数组。

所以做一点改变,你会很高兴:

$this->validate($request, array(  // Removed `[]` from the array.
    'tenant_id' => 'required',
    'asset_id' => 'required',
    'description' => 'required',
    'address' => 'required',
    'price' => 'required',
    'occupation_date' => 'required',
));

答案 1 :(得分:0)

以上答案是正确的,这是另一种解决laravel 5.5验证问题的方法我问过

$validation = validator::make($request->all(), [
        'tenant_id' => 'required',
        'asset_id' => 'required',
        'description' => 'required',
        'address' => 'required',
        'price' => 'required',
        'occupation_date' => 'required',
    ]);

有关详细信息,请访问https://laravel.com/docs/5.5/validation#manually-creating-validators

答案 2 :(得分:0)

 $request->validate([ 
'0'=>'',
'tenant_id' => 'required',
'asset_id' => 'required',
'description' => 'required',
'address' => 'required',
'price' => 'required',
'occupation_date' => 'required',]);