Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 Multiple Fields Validation

I have a field in the view the go something like

<div>
  <input type="text" name="new[0][description]" id="description-new-0">
  <input type="text" name="new[0][amount]" id="amount-new-0">
</div>

<div>
  <input type="text" name="new[1][description]" id="description-new-1">
  <input type="text" name="new[1][amount]" id="amount-new-1">
</div>

and so on... so you can imagine that its a dynamic field that adds in the form every time you tick the add button or whatever.

The question is how can I VALIDATE these dynamic fields and will return the right error for each fields?

Thanks!

Note this is Laravel 5 but if you have a Laravel 4 solution similar to this one I guess(really guess) it will work.

Thanks guys!

like image 469
Einlanzer Avatar asked May 16 '15 00:05

Einlanzer


1 Answers

create in folder Requests validator for this form like

<?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;
    }
}

more info How To: Validate an array of form fields with Laravel

then in view do the next

@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

so you add block with error message for each block with inputs. In my example only description processed, amount you can processed similar description For my it works and look like enter image description here

UPD: Laravel version 5.2 has array validation so you can create request validator like:

public function rules()
{
    return [
        'names.*' => 'required|max:50',
        'emails.*' => 'required|max:100',
    ];
}

for more info read DOC

like image 165
dyachenko Avatar answered Oct 18 '22 19:10

dyachenko