2016-07-19 25 views
0

看來我的驗證不是很正確,所以林試圖存儲費用到我的數據庫時,我得到這個錯誤信息。我試過驗證,我得到這個mesaage:方法[validateString]不存在

我以前曾經檢查過相關的問題,但是沒有太大的幫助。我創建FeeValidator延伸驗證與此陣:

<?php namespace \Events\Services\Validations; 

    use \Events\Services\Validations\AbstractValidator as Validator; 

    class FeeValidator extends Validator { 
     protected $rules = array(
      'title'   =>  'required', 
      'price'   =>  'required|numeric', 
      'quantity'  =>  'integer', 
      'valid_from' =>  'date', 
      'valid_to'  =>  'date', 
      'ticket_limit' =>  'integer', 
      'url_redirect' =>  'string', 
     ); 
    } 

然後我在FeesRepository類此店()函數一個FeesRepository:

<?php namespace \Events\Repositories; 

use \Events\Models\Fee as Fees; 
use \Events\Models\Event as Event; 
use \Events\Repositories\Contracts\FeesRepositoryInterface; 
use \Events\Services\Validations\FeeValidator; 

class FeeRepository implements FeesRepositoryInterface { 

    protected $theme; 

    protected $feeValidator; 

public function store($eventId) { 

     $validation = $this->feeValidator->with(\Input::all()); 

     if ($validation->passes()) { 
      $fee = new Fees; 
      $fee->event_id   = $eventId; 
      $fee->title    = \Input::get('title'); 
      $fee->price    = \Input::get('price'); 
      $fee->quantity   = \Input::get('quantity') == "" ? 1 : \Input::get('quantity'); 
      $fee->discount   = \Input::get('discount'); 
      $fee->valid_from  = \Input::get('valid_from'); 
      $fee->valid_to   = \Input::get('valid_to'); 
      $fee->coupon   = \Input::get('coupon') == "" ? null : \Input::get('coupon'); 
      $fee->tickets_limit  = \Input::get('ticket_limit'); 
      $fee->url_redirect  = \Input::get('url_redirect'); 
      $fee->save(); 

      $id = $fee->event->id; 
      if ($fee->event->eventable_type == '\Events\Models\Training') 
      { 
       return \Redirect::route('admin.training.edit' , array($id)); 
      } 
      elseif($fee->event->eventable_type == '\Events\Models\Meetup') { 
       return \Redirect::route('admin.meetup.edit' , array($id)); 
      } 
      else 
      { 
       return \Redirect::route('admin.conference.edit' , array($id)); 
      } 

     } 

      else { 
          return \Redirect::back()->withInput()->withErrors($this->feeValidator->errors()); 
       } 

    } 

} 

最後你可以在FeesController

<?php namespace \Events\Controllers; 

use \Events\Repositories\FeeRepository as Fee; 
use \Events\Models\Fee as Fees;use \Events\Services\Validations\FeeValidator; 


    class FeesController extends \BaseController { 

     protected $fee; 

     protected $feeValidator; 

     public function __construct(Fee $fee , FeeValidator $feeValidator) 
     { 
      $this->fee = $fee; 
      $this->feeValidator = $feeValidator; 
     } 

    public function store($eventId) { 
        return $this->fee->store($eventId); 
      } 
     } 

林不知道是什麼問題。有人可以給我一個提示! 謝謝

+0

您收到了什麼錯誤? – Daniel

+0

發佈有問題的錯誤。 –

回答

0

爲了您的驗證,你可以在你的形式,使用已建立Laravel https://laravel.com/docs/5.2/validation默認的驗證顯示錯誤,你可以使用JS或者乾脆例如:

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}"> 
    <label for="email" class="col-md-4 control-label">E-Mail Address</label> 
<div class="col-md-6"> 
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}"> 
@if ($errors->has('email')) 
    <span class="help-block"> 
    <strong>{{ $errors->first('email') }}</strong> 
    </span>@endif 
</div> 
</div> 

編輯:嘿,你有簡單的驗證,所以你有一個簡單的方法來使用這種驗證,但你的方式是正確的,但爲什麼你不能嘗試簡單的方法? 這適用於Laravel 4.2使用驗證示例的簡單方法

Route::post('register', function() 
{ 
    $rules = array(...); 

    $validator = Validator::make(Input::all(), $rules); 

    if ($validator->fails()) 
    { 
     return Redirect::to('register')->withErrors($validator); 
    } 
}); 
+0

我正在使用Laravel 4.2,我正在嘗試什麼不工作,它不工作。試了這兩個建議。也許陣列** $規則**沒有正確定義。 –

+0

@ ajax_27 chek mayb它會幫助你 – Hamelraj

+0

我嘗試驗證,就像你建議我一樣,它確實工作,但我的項目是以另一種方式構建的,所以我試圖遵循該約定。問題是Laravel 4.2驗證規則。再次感謝您的幫助! –

0

我不知道你在那裏使用的框架,但這裏是我的laravel驗證

使用Validator;

$rules = array(
     'token' => 'required|min:5|max:50|string' 
    ); 
    // Create a new validator instance from our validation rules 
    $validator = Validator::make(Input::all(), $rules); 


    // If validation fails, we'll exit the operation now. 
    if ($validator->fails()) { 
     return $validator->messages()->toJson(); 
    } 
相關問題