2016-02-15 81 views
0

我需要在我的一個控制器中進行驗證 - 我無法爲這個特定問題使用請求類 - 所以我想弄清楚如何定義自定義驗證消息在控制器中。Larval 5.2:在控制器中定義自定義錯誤消息

我已經找遍了,找不到任何暗示它可能的東西。

可能嗎?我會怎麼做?

public function store(Request $request) 
{ 
    $this->validate($request, [ 
     'title' => 'required|unique:posts|max:255', 
     'body' => 'required', 
    ]); 

    // can I create custom error messages for each input down here? 
    // something like... 

    $this->validate($errors, [ 
     'title' => 'Please enter a title', 
     'body' => 'Please enter some text', 
    ]); 
} 

回答

0

您應該有像下面這樣的請求類。信息覆蓋是你在找什麼。

class RegisterRequest extends Request 
{ 
    public function authorize() 
    { 
     return true; 
    } 

    public function rules() 
    { 
     return [ 
      'UserName'  => 'required|min:5|max:50', 
      'Password'  => 'required|confirmed|min:5|max:100', 

     ]; 
    } 

    public function response(array $errors){ 
     return \Redirect::back()->withErrors($errors)->withInput(); 
    } 

    //This is what you are looking for 
    public function messages() { 
     return [ 
      'FirstName' => 'Only alphabets allowed in First Name', 
     ]; 
    } 
} 
+0

我不能使用請求類對於此問題,它在控制器來完成。 – timgavin

0

這做到了

$this->validate($request, [ 
    'title' => 'required', 
    'body' => 'required', 
], [ 
    'title.required' => 'Please enter a title', 
    'body.required' => 'Please enter some text', 
]);