tạo trong yêu cầu thư mục validator cho hình thức này như
<?php
use Illuminate\Foundation\Http\FormRequest;
class MultipleRequest 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 = [
'description' => 'required|array',
];
if ($this->request->get('description')) {
foreach($this->request->get('description') as $key => $val)
{
$rules['description.'.$key] = 'required|min:7'; //example
}
}
return $rules;
}
public function messages()
{
$messages = [];
if ($this->request->get('description')) {
foreach ($this->request->get('description') as $key => $val) {
$messages['description.' . $key . '.min'] = 'Wrong field.';
$messages['description.' . $key . '.required'] = 'This field required.';
}
}
return $messages;
}
}
chi tiết How To: Validate an array of form fields with Laravel
sau đó theo quan điểm làm tiếp theo
@if (Session::has('_old_input'))
@for ($i=0; $i<count(Session::get('_old_input.description')); $i++)
<div>
@if($errors->any() && Session::get('errors')->getBag('default')->has('description.' . $i))
<p class="">{{Session::get('errors')->getBag('default')->first('description.' . $i)}}</p>
@endif
<input type="text" name="new[][description]" id="description-new-{{$i}}" value="{{Session::get('_old_input.description.' . $i)}}">
<input type="text" name="new[][amount]" id="amount-new-{{$i}}" value="{{Session::get('_old_input.amount.' . $i)}}">
</div>
@endfor
@endif
vì vậy bạn có thêm khối với thông báo lỗi cho từng khối với đầu vào. Trong ví dụ của tôi chỉ mô tả xử lý, số tiền bạn có thể xử lý mô tả tương tự Đối với tôi nó hoạt động và trông giống như
UPD: Laravel phiên bản 5.2 có xác nhận mảng, do đó bạn có thể tạo yêu cầu xác nhận như:
public function rules()
{
return [
'names.*' => 'required|max:50',
'emails.*' => 'required|max:100',
];
}
để biết thêm thông tin, đọc DOC
Bạn có thể sử dụng ký hiệu dấu chấm để truy cập các mục mảng lồng nhau trong quá trình định giá, do đó hãy tạo quy tắc cho 'new.description' và' n ew.amount' – Azeame