2016-09-26 250 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不支持此驗證規則。 –