以动态形式验证Laravel 5.5

时间:2017-09-06 09:00:51

标签: php laravel laravel-5

如何为动态表单创建验证?

//Controller
public function store(Request $request)
{       
    $rules = [
        'companyName' => 'required',
        'bannerName' => 'required',
        'bannerDescription' => 'required',
        'bannerURL' => 'required',
        'bannerImg' => 'required',
    ];

    $customMessages = [
        'companyName.required' => 'Yo, what should I call you?',
        'bannerName.required' => 'Yo, what should I call you?',
        'bannerDescription.required' => 'Yo, what should I call you?',
        'bannerURL.required' => 'Yo, what should I call you?',
        'bannerImg.required' => 'Yo, what should I call you?',
    ];

    $this->validate($request, $rules, $customMessages);
}

这是我的看法,我的名字" attr是数组因为我将大量数据存储到DB。点击" +添加横幅" jquery将使用相同的4个输入克隆div。 如果我删除数组,一切都会工作,但如果没有我会得到以下错误

  

htmlspecialchars()期望参数1为字符串,给定数组为

{!! Form::open(['action' => 'CompanyController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
                        <div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
                            <label for="companyName">Company URL Address</label>    
                                <input type="text" class="form-control" value="{{old('companyName')}}" id="companyName" name="companyName" placeholder="example.com">
                                <small class="text-danger">{{ $errors->first('companyName') }}</small>        
                        </div>

                        <hr>

                         <div data-sel-baner-box>                         
                            <div data-sel-baner-form>
                                    <div class="panel-heading"><h4 style="text-align: center">New banner</h4></div>

                                    <div class="form-group{{ $errors->has('bannerName') ? ' has-error' : '' }}">
                                        <label for="bannerName">Title</label>     
                                        <input type="text" class="form-control" id="bannerName" value="{{old('bannerName')}}" name="bannerName[]" placeholder="Name">
                                        <small class="text-danger">{{ $errors->first('bannerName') }}</small>
                                    </div>

                                    <div class="form-group{{ $errors->has('bannerDescription') ? ' has-error' : '' }}">
                                        <label for="bannerDescription">Banner Description</label>
                                        <input type="text" class="form-control" id="bannerDescription" value="{{old('bannerDescription')}}" name="bannerDescription[]" placeholder="Description">
                                        <small class="text-danger">{{ $errors->first('bannerDescription') }}</small>
                                    </div>

                                    <div class="form-group{{ $errors->has('bannerURL') ? ' has-error' : '' }}">
                                        <label for="bannerURL">Banner URL</label>
                                        <input type="text" class="form-control" id="bannerURL" value="{{old('bannerURL')}}" name="bannerURL[]" placeholder="URL">
                                        <small class="text-danger">{{ $errors->first('bannerURL') }}</small>
                                    </div>

                                    <div class="form-group{{ $errors->has('bannerImg') ? ' has-error' : '' }}">
                                        <label for="bannerImg">File input</label>
                                        <input type="file" class="form-control-file" id="bannerImg" name="bannerImg[]">
                                        <small class="text-danger">{{ $errors->first('bannerImg') }}</small>
                                   </div>
                               </div>

                       <a href="##" data-sel-btn-add-baner class="btn btn-primary">+ Add Banner</a>
                       <button type="submit" class="btn btn-primary">Save Company</button> 

                    {!! Form::close() !!}       

有任何提示吗?

1 个答案:

答案 0 :(得分:2)

您必须将数组输入验证规则的点表示法用作:

$rules = [
    'companyName.*' => 'required',
    'bannerName.*' => 'required',
    'bannerDescription.*' => 'required',
    'bannerURL.*' => 'required',
    'bannerImg.*' => 'required',
];

您可以查看文档here