6

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!

1
  • You should be able to use dot notation to access the nested array items during valuation, so create your rules for new.description and new.amount Commented May 16, 2015 at 15:07

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.