2017-04-25 98 views
0

如何可以验证与像相同名称的多个输入:如何验证阵列中Laravel 5.4

{!! Form::number('amount[]', $item->amount, ['class'=>'form-control width_30', 'placeholder'=>'Amount']) !!} 

在请求?

我才发现,它可以与数字验证阵列像

{!! Form::number('amount[1]', $item->amount, ['class'=>'form-control width_30', 'placeholder'=>'Amount']) !!} 
{!! Form::number('amount[2]', $item->amount, ['class'=>'form-control width_30', 'placeholder'=>'Amount']) !!} 

的方法,但我必须添加和动态删除领域,所以这种方法不会为我工作。

这是在我的要求规则的代码:

public function rules() 
{ 
    return [ 
     // 
     'client'=>'required', 
     'product'=>'required|array', 
     'amount'=>'required|array', 
     'item_id' => 'required|array' 
    ]; 
} 

,当我用这个required|array方法,它返回我一个错误:

ErrorException in HtmlBuilder.php line 65: 
htmlentities() expects parameter 1 to be string, array given (View: C:\xampp\htdocs\smk\resources\views\admin\sales\edit.blade.php) 
+0

您是否将数组保存在'$ item-> amount'中?如果是这样,那么我想这个问题在这里,'Form :: number('field_name','default_value')''预计'default_value'是一个字符串,也许你传入一个数组(在'$ item->金额') –

回答

0

只需添加*在你的验证。

public function rules(){ 
    return ['amount.*'=>'required|array' ]; 
} 
+0

它不工作,laravel还给我这个“htmlentities()期望参数1是字符串,数组给出” – chengwei