Add validation to repeater field

Posted 9 months ago by GregorSams

I am using Laravel 5.5 and I am using the jquery repeater field.

Find below my frontend:

    @extends('layouts.app') @section('content')
    <div class="panel-body">
        {{-- display success message --}} @if (Session::has('success'))
        <div class="alert alert-success">
            <strong>Success:</strong> {{ Session::get('success') }}
        </div>
        @endif {{-- display error message --}} @if (count($errors) > 0)
        <div class="alert alert-danger">
            <strong>Error:</strong>
            <ul>
                @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif
    </div>
    
    <!-- Form -->
    <form action="{{ route('instrument.updateDetails', [$instrumentUnderEdit->id]) }}" method='POST' class="repeater" enctype="multipart/form-data">
        {{ csrf_field() }}
        <label>Firstname</label>
        <label>Lastname</label>
        <label>Twitter</label>
        <div data-repeater-list="team">
            <div data-repeater-item>
    
                <input type="text" name="firstName" value='{{ $instrumentUnderEdit->firstName }}' />
    
                <input type="text" name="lastName" value='{{ $instrumentUnderEdit->lastName }}' />
    
                <input type="text" name="twitter" value='{{ $instrumentUnderEdit->twitter }}' />
    
                <input data-repeater-delete type="button" value="Delete" />
            </div>
        </div>
        <input data-repeater-create type="button" value="Add" />
        </br>
        </br>
        <div class="row">
            <div class="form-group col-xs-5 col-lg-1">
                <label>User Name*</label>
                <input type="text" name="userName" class="form-control" placeholder="Insert your contributor name" style="width: 250px;"> </div>
        </div>
        <input type="hidden" name='_method' value='POST'>
        <input type="submit" class='btn btn-success btn-sm' value='Update'>
    </form>
    @endsection

I am using the following in my backend for validation:

        $this->validate($request, [
            'firstName' => 'nullable|min:2|max:190',
            'lastName' => 'nullable|min:3|max:190',
            'twitter' => 'nullable|min:3|max:190',
            'userName' => 'required|min:1|max:190',
        ]);

Currently, the repeater field does not valide correctly. I am guessing that the reason for this is that in the request the name attribute is rendered as the following f.ex.: "team[1][firstName]", "team[2][firstName]" whereas the number indicates the row of the field.

Any suggestions how to add validation to each repeater field?

Thx in advance for your replies!

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.