2017-09-06 76 views
0

如何爲動態表單創建驗證?以動態形式驗證Laravel 5.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是數組,因爲我將大量的數據存儲到數據庫。點擊「+添加橫幅」jquery將克隆與相同的4個輸入。 如果我要刪除陣列,一切都會OFC,但如果沒有,我會收到以下錯誤

用htmlspecialchars()預計參數1是字符串數組給定

{!! Form::open(['action' => '[email protected]', '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() !!}  

任何請提示一下?

回答

2

你必須使用點符號數組輸入驗證規則:

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

你可以看到文檔here

+0

感謝您的回覆。 我試圖做到這一點,但它不工作,我猜。仍然有同樣的錯誤。 – qqmydarling

+0

@qqmydarling驗證的方式是我上面說過的,你的錯誤是在html視圖中由於一些空的數據或其他東西。 –

+0

你可以添加錯誤是在哪一行? –