FormRequests in Laravel 5 is good approach for validation and authorizing. But how to procceed if I must validate request that contain data for one to many relationship. For example if I have simple invoice app. One invoice has many services. My form post request contain this data:
array (size=5)
'date' => string '2014-11-14' (length=10)
'num' => string '175' (length=3)
'client_id' => string '5' (length=1)
'vat' => string '1' (length=1)
'services' =>
array (size=2)
0 =>
array (size=3)
'description' => string 'Service 1' (length=36)
'value' => string '10' (length=2)
'items' => string '2' (length=1)
1 =>
array (size=3)
'description' => string 'Service 2' (length=11)
'value' => string '20' (length=2)
'items' => string '2' (length=1)
Now in InvoiceFormRequest class I can validate invoice data, but how to proceed with services:
<?php namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Response;
class InvoiceFormRequest extends FormRequest
{
public function rules()
{
return [
'date' => 'required',
'num' => 'required',
'client_id' => 'required',
'vat' => 'required'
];
}
public function authorize()
{
return true;
}
}
Thanks in advance!
Update: As I've read here in Laravel 5.2 will can write something like this:
'services.*.description' => 'required',
'services.*.value' => 'required:numeric',
'services.*.items' => 'required:integer'