Laravel验证动态表单输入数据?

时间:2016-08-30 06:19:18

标签: arrays laravel laravel-validation dynamicform

我使用动态表单输入插入数组数据但是对数组的验证对我不起作用。没有输入它在数据库中保存为空白值。

在表格列中,我有像这样的惰性表单输入,使用vueJs添加动态表单字段:

<tr v-for="row in rows">
 <td>{!! Form::text('description[]',null,['class' => 'input-field input-sm','v-model'=>'row.description']) !!}
@if ($errors->has('description'))
       <span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('description') }}"></i></span>@endIf</td>
<td>{!! Form::text('log_time[]',null,['class' => 'input-field input-sm','v-model'=>'row.log_time']) !!}
      @if ($errors->has('log_time'))<span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('log_time') }}"></i></span>@endIf </td>
<td> <a @click="removeRow(row)"><button class="btn btn-danger" type="button" id="dim">
            <span class="glyphicon glyphicon-minus"></span></button> </a>
             <a @click="addRow"><button class="btn btn-success" type="button" id="dim">
              <span class="glyphicon glyphicon-plus"></span></button></a>
</td>
</tr>

我的控制器存储功能:

protected $rules = [
        'row.description' => ['required|array'],
        'row.log_time' => ['required|array'],
    ];
public function store(PslCall $call,Request $request)
    {
        $this->validate($request, $this->rules);
        $data = array();
        foreach($request->description as $key=>$value){
            $data[]=[
                'description'=> $value,
                'log_time'=> $request->log_time[$key],
                'call_id'=>$call->id,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now(),
            ];
        }

        PortLog::insert($data);
        return Redirect::route('calls.logs.index',$call->id)->with('message','You have successfully submitted');
    }

在这里你可以检查我的dd()没有输入我可以插入:(为此我必须给予验证;

array:2 [▼
  0 => array:5 [▼
    "description" => ""
    "log_time" => ""
    "call_id" => 2
    "created_at" => Carbon {#351 ▶}
    "updated_at" => Carbon {#352 ▶}
  ]
  1 => array:5 [▼
    "description" => ""
    "log_time" => ""
    "call_id" => 2
    "created_at" => Carbon {#353 ▶}
    "updated_at" => Carbon {#354 ▶}
  ]
]

1 个答案:

答案 0 :(得分:3)

您的$rules尝试

存在问题
protected $rules = [
        'description.*' => 'required|min:1',
        'log_time.*' => 'required|min:1',
    ];