2016-04-12 74 views
1

我有一個簡單的測驗應用程序,我正在寫一系列的挑戰(簡單,中等,困難),每個都有自己的問題。Laravel - 獨特的規則驗證 - getMessages不存在

要添加的問題應該是唯一的。

我有下面的代碼爲「存儲」數據

$v = ChallengeQuests::validate(Input::all()); 

     if ($v->passes()) { 
      print 'validate passed'; 
      $record = ChallengeQuests::create(array(
       'challenge_id'=> (int) Input::get('challenge_id'), 
       'question_id'=> (int) Input::get('question_id') 
      )); 
      $record->save(); 
      return redirect()->to($url['redirects_to']); 
     } else { 
      print 'error'; 
      print_r($v->getMessages()); 
      return Redirect::to('/')->withErrors($v->getMessages()); 
     } 

在我的模型我有一個驗證方法

// model 
class ChallengeQuests extends Model 
{ 
    // 
    protected $table = 'challengequests'; 
    protected $fillable=[ 
     'challenge_id', 
     'question_id' 
    ]; 

    public static function validate($input) { 
     $rules = array(
      'challenge_id' => 'Required|Integer', 
      'question_id' => 'Required|Integer|Unique:questions,id' 
     ); 

     return Validator::make($input, $rules); 
    } 
} 

但是當我運行我的代碼,Laravel抱怨

BadMethodCallException in Validator.php line 3016: 
Method [getMessages] does not exist. 

我想讓它使得question_id是唯一的。

我在做什麼錯?

編輯:

我使用:

use Illuminate\Support\Facades\Validator; 
use Illuminate\Support\Facades\Input; 

堆棧跟蹤:

BadMethodCallException in Validator.php line 3016: 
Method [getMessages] does not exist. 
in Validator.php line 3016 
at Validator->__call('getMessages', array()) in ChallengeQuestionsController.php line 78 
at ChallengeQuestionsController->store(object(Request)) 
at call_user_func_array(array(object(ChallengeQuestionsController), 'store'), array(object(Request))) in Controller.php line 80 
at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 146 
at ControllerDispatcher->call(object(ChallengeQuestionsController), object(Route), 'store') in ControllerDispatcher.php line 94 
at ControllerDispatcher->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 
at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 96 
at ControllerDispatcher->callWithinStack(object(ChallengeQuestionsController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 54 
at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\ChallengeQuestionsController', 'store') in Route.php line 174 
at Route->runController(object(Request)) in Route.php line 140 
at Route->run(object(Request)) in Router.php line 724 
at Router->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 
at Pipeline->then(object(Closure)) in Router.php line 726 
at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 699 
at Router->dispatchToRoute(object(Request)) in Router.php line 675 
at Router->dispatch(object(Request)) in Kernel.php line 246 
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 52 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) 
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32 
at Pipeline->Illuminate\Routing\{closure}(object(Request)) 
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 
at Pipeline->then(object(Closure)) in Kernel.php line 132 
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 99 
at Kernel->handle(object(Request)) in index.php line 53 
at require_once('/Applications/XAMPP/xamppfiles/htdocs/laravel/scquiz/public/index.php') in server.php line 21 
+0

請發佈錯誤的整個堆棧跟蹤。 – Bogdan

+0

按要求添加堆棧跟蹤 – zardon

回答

3

用於獲取驗證消息正確的方法是不getMessages()它的messages(),使你的代碼應該是這樣的:

return Redirect::to('/')->withErrors($v->messages()); 

此外,如果你正在使用Laravel 5,你可能要考慮使用Form Request Validation這確實你想實現,但在一個更好的方式同樣的事情,並處理不同的層上的驗證,服用關心爲您傳遞錯誤和頁面重定向。


通過使用你的情況表單請求,控制器方法將減少到這一點:

public function store(ChallangeQuestsFormRequest $request) 
{ 
    ChallengeQuests::create($request->only('challenge_id', 'question_id')); 

    return redirect()->to($url['redirects_to']); 
} 

由於規則和驗證,以及在發生錯誤時重定向,將被處理由ChallangeQuestsFormRequest類。另外,使用create創建模型條目將自動保存條目,因此create的結果不需要使用save

+0

您的解決方案可以正常工作,並且我可以通過'print_r'原始輸出消息。我認爲Form Request Validation對我來說可能有點多,因爲我還在學習Laravel。我一直無法獲取頁面來呈現錯誤消息,但我想我可以通過Flash消息解決這個問題。 – zardon

+0

表單請求並不那麼複雜,它們只是一個你作爲參數注入控制器方法的類(如我的答案中所示),其中包含規則併爲您處理驗證。如果驗證通過,則控制器方法代碼將被執行,如果失敗,它將重定向回您的表單,將錯誤很好地添加到{{$ errors}}變量中作爲數組顯示在視圖中。如果您有時間,我建議您至少閱讀我鏈接的文檔,因爲它只有幾個段落,並且您可能從一開始就掌握它。 – Bogdan

+0

此外,Laravel還附帶了一個名爲'dump'的好助手函數,它與'print_r'類似,但它使用正確的縮進和顏色很好地格式化輸出。 – Bogdan