doutouhe5343 2018-01-14 19:04
浏览 25
已采纳

添加验证到转发器字段

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!

  • 写回答

1条回答 默认 最新

  • douweibiao8471 2018-01-14 19:26
    关注

    You would validate a field array like this as outlined in the Laravel docs

    ‘team.*.firstName’ => ‘your rules’
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作