2016-02-15 90 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', 
]);