我使用动态表单输入插入数组数据,但数组验证不适用于我。无需输入将其保存在数据库中作为空白值。
在表格列中,我有这样的惰性表单输入,使用 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 ▶}
]
]