Laravel 验证动态表单输入数据?
Posted
技术标签:
【中文标题】Laravel 验证动态表单输入数据?【英文标题】:Laravel validation for dynamic form input data? 【发布时间】:2017-01-06 07:15:54 【问题描述】:我正在使用 动态 表单输入插入 数组数据,但数组验证不适用于我。没有输入它在数据库中保存为空白值。
在表格列中,我有这样的惰性表单输入,使用 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 ▶
]
]
【问题讨论】:
您能保证,$request
包含您要验证的所有值吗?
@SafoorSafdar 现在检查问题
看起来,您需要从验证规则中删除 row.
我删除了但它不起作用
【参考方案1】:
你的$rules
有问题试试
protected $rules = [
'description.*' => 'required|min:1',
'log_time.*' => 'required|min:1',
];
【讨论】:
如果描述字段是一个数组,比如'description':['value':'testing','value':'description content']可以验证为@987654323 @但我认为不是这种情况..以上是关于Laravel 验证动态表单输入数据?的主要内容,如果未能解决你的问题,请参考以下文章