laravel 4 返回验证消息错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17134390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 4 returning validation message error
提问by Web Student
I am currently just testing laravel 4, but i have a problem, in the laravel docs returning error messages is described this way $messages->first('email');
should return the message, but no matter what methog id try on the messages
i get error
我目前只是在测试 Laravel 4,但我有一个问题,在 Laravel 文档中,返回错误消息的描述以这种方式$messages->first('email');
应该返回消息,但无论尝试什么方法,messages
我都会收到错误
my cobtroller
我的cobtroller
public function postSignup()
{
$rules = array(
'display_name' => 'required|unique:users',
);
$messages = array(
'display_name.required' => 'Felhasználónév k?telez?',
'display_name.unique' => 'Ez a Felhasználónév foglalt',
);
$val = Validator::make(Input::all(), $rules, $messages);
if ($val->passes())
{
$data = array('msg' => 'yay');
}
else
{
print_r($messages->first('display_name'));
}
return Response::json($data);
}
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function first() on a non-object"
{“错误”:{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function first() on a non-object"
if i try with all
just for a test print_r($messages->all()); im getting the following
如果我all
只是为了测试而尝试print_r($messages->all()); 我得到以下
{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","message":"Call to a member function all() on a non-object"
could please someone point out what i am doing wrong?
请有人指出我做错了什么吗?
回答by The Alpha
You may try this
你可以试试这个
if ($val->passes())
{
$data = array('msg' => 'yay');
}
else
{
$messages = $validator->messages();
$data = array('msg' => $messages->first('display_name'));
}
return Response::json($data);
print_r(...);
in the controller
will print the output outside of the template. On the client side you can check the msg
something like this (using jQuery
for example)
print_r(...);
在controller
将打印模板外的输出。在客户端,您可以检查这样的msg
事情(jQuery
例如使用)
$.get('url', function(data){
if(data.msg == 'yay')
{
// success
}
else
{
// failed, so data.msg contains the first error message
}
});
回答by Chris Breslaw
Validator::make method only accepts two arguments.
Validator::make 方法只接受两个参数。
回答by Blessing
You are accessing the messages array you passed into the validator, not the error messages the validator created. Change your code to this:
您正在访问传递给验证器的消息数组,而不是验证器创建的错误消息。将您的代码更改为:
$val->messages()->first('email')
NOTE: The validator::make method accepts 3 arguments:
注意:validator::make 方法接受 3 个参数:
/**
* Create a new Validator instance.
*
* @param array $data
* @param array $rules
* @param array $messages
* @return \Illuminate\Validation\Validator
*/
public function make(array $data, array $rules, array $messages = array())