添加验证到转发器字段

时间:2018-01-14 19:04:40

标签: php laravel laravel-5

我正在使用Laravel 5.5,而我正在使用jquery repeater field

在下面找到我的前端:

@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

我在后端使用以下内容进行验证:

    $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',
    ]);

目前,转发器字段无法正确显示。我猜这是因为在请求中name属性呈现为以下f.ex。:"team[1][firstName]""team[2][firstName]"而数字表示该字段的行

如何为每个转发器字段添加验证?

提前回复您的回复!

1 个答案:

答案 0 :(得分:1)

您将验证Laravel文档

中概述的字段数组
‘team.*.firstName’ => ‘your rules’

https://laravel.com/docs/5.5/validation#validating-arrays