2016-09-26 252 views
0

您好我试图验证阵列输入和选择这样来验证阵列输入:如何使用proengsoft/laravel-jsvalidation和Laravel 5

<td width="23%">            
{!!Form::select('mmscod_id[]',['' => '- Seleccione un material -'] +$mat,null,['class' => 'form-control', 'id'=>'mmscod_id'])!!}  
</td> 

<td width="17%"> 
<input type="text" class="form-control" id="cantidad" name="vtcanp[]"/> 

</td> 
<td width="17%"> 
<input type="text" class="form-control" id="precio" name="vtprep[]"/> 
</td> 

我使用proengsoft/laravel-jsvalidation为客户端验证。对于后端,我使用Laravel的Form请求。

我也用这个网站的方法:How To: Validate an array of form fields with Laravel,但它不工作,并抛出错误:

error1

error2

编辑: 我忘了提,这些元素动态创建 请帮帮我

回答

0

Laravel su验证数组输入的端口。你需要使用这个约定来验证数组项目。

$rules = [ 
    'vtcanp.*' => 'required', 
]; 

例如:

这是我的自定义请求类

class InvoiceRequest extends FormRequest 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     $rules =[ 
      'client' => 'required', 
      'date' => 'required', 
      'total' => 'required', 
      'discount' => 'required', 
      'item.*' => 'required', 
      'rate.*' => 'required', 
      'quantity.*' => 'required', 


     ]; 


     return $rules; 
    } 
} 

并在视图中添加

{!! JsValidator::formRequest('App\Http\Requests\InvoiceRequest') !!} 

这些验证并显示与输入的位置的误差信息我动态添加到视图中的数组。

+0

请尝试解释您的代码 – TheGameiswar

+0

但是在laravel 5.1中,该规则不起作用=( –

+0

我试过并且没有验证客户端,我认为proengsoft/laravel-jsvalidation不支持此验证规则。 –