我正在使用Laravel 5.5
,我正在使用jquery中继器字段。
在我的前端下面找到:
@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]"
而数字表示字段的行。
任何建议如何为每个中继器字段添加验证?
提前感谢您的回复!
您将验证Laravel文档中概述的这样的字段数组
‘team.*.firstName’ => ‘your rules’
https://laravel.com/docs/5.5/validation#validating-arrays