2014-07-02 151 views
0

我使用parsley.js客戶雙面驗證,也想用它爲服務器雙面驗證(Laravel表單驗證)顯示錯誤使用歐芹造型。所以它是統一的。Parsley.js和Laravel校驗錯誤

因此,只要我的服務器端驗證失敗,我想顯示與正確字段相關的特定錯誤。我不想使用遠程庫。

有沒有一個例子如何做到這一點?

鏈接:Laravel ValidatorParsley

非常感謝您!

回答

1

你可以做這樣的事情:

<div> 
    {{ Form::open(['route' => 'dashboard.testform', 'id' => 'testForm']) }} 
    <div class="form-group row"> 
     {{ Form::label('field1', 
      'Field 1', 
      ['class' => 'col-xs-2'] 
     ) }} 
     <div class="col-xs-10"> 
      <?= Form::text('field1', null, ['class' => 'col-xs-12 required ' . ($errors->has('field1') ? 'parsley-error' : '') ]) ?> 
      @if ($errors->has('field1')) 
       <ul class="parsley-errors-list filled"> 
        {{ $errors->first('field1', '<li class="parsley-required">:message</li>') }} 
       </ul> 
      @endif 
     </div> 
    </div> 
    <div class="form-group row"> 
     {{ Form::label('field2', 'Field 2', ['class' => 'col-xs-2']) }} 
     <div class="col-xs-10"> 
      <?= Form::text('field2', null, ['class' => 'col-xs-12 ' . ($errors->has('field2') ? 'parsley-error' : '') ]) ?> 
      @if ($errors->has('field2')) 
       <ul class="parsley-errors-list filled"> 
        {{ $errors->first('field2', '<li class="parsley-required">:message</li>') }} 
       </ul> 
      @endif 
     </div> 
    </div> 
    <div class="form-group row"> 
     {{ Form::label('field3', 'Field 3', ['class' => 'col-xs-2']) }} 
     <div class="col-xs-10"> 
      <?= Form::text('field3', null, ['class' => 'col-xs-12 required ' . ($errors->has('field3') ? 'parsley-error' : '') ]) ?> 
      @if ($errors->has('field3')) 
       <ul class="parsley-errors-list filled"> 
        {{ $errors->first('field3', '<li class="parsley-required">:message</li>') }} 
       </ul> 
      @endif 
     </div> 
    </div> 
    {{ Form::submit('Submit form', ['class' => 'btn btn-primary']) }} 
    {{ Form::close() }} 
</div> 

{{ HTML::style('http://parsleyjs.org/src/parsley.css') }} 
{{ HTML::script('http://code.jquery.com/jquery-1.11.1.min.js') }} 
{{ HTML::script('http://parsleyjs.org/dist/parsley.min.js') }} 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#testForm").parsley(); 
    }); 
</script> 

而且你的控制器看起來就像這樣:

public function testform() { 
    $rules = array(
     'field1' => 'date', 
     'field2' => 'required' 
    ); 

    $validator = Validator::make(
     Input::all(), 
     $rules 
    ); 
    if ($validator->fails()) { 
     return Redirect::back()->withErrors($validator); 
    } 

    return Redirect::back(); 
} 

這可能是一個好主意,以創建一個custom Form Macro是回聲的輸入錯誤,如果存在。這樣你的觀點被簡化了。