1 year ago

Add validation to repeater field

Posted 1 year ago by GregorSams

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

Find below my frontend:

    @extends('') @section('content')
    <div class="panel-body">
        {{-- display success message --}} @if (Session::has('success'))
        <div class="alert alert-success">
            <strong>Success:</strong> {{ Session::get('success') }}
        @endif {{-- display error message --}} @if (count($errors) > 0)
        <div class="alert alert-danger">
                @foreach($errors->all() as $error)
                <li>{{ $error }}</li>
    <!-- Form -->
    <form action="{{ route('instrument.updateDetails', [$instrumentUnderEdit->id]) }}" method='POST' class="repeater" enctype="multipart/form-data">
        {{ csrf_field() }}
        <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" />
        <input data-repeater-create type="button" value="Add" />
        <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>
        <input type="hidden" name='_method' value='POST'>
        <input type="submit" class='btn btn-success btn-sm' value='Update'>

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.