php 从 Laravel 验证中获取错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35421088/
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
Get error message from Laravel validation
提问by ZZZ
I use Laravel built-in validator and I want to get the first error message
我使用 Laravel 内置验证器,我想获得第一条错误消息
if ($validator->fails()) {
$error = $validator->messages()->toJson();
.....
}
This is the result when I print error
这是我打印错误时的结果
{"name":["The name must be at least 5 characters."],"alamat":["The address must be at least 5 characters."]}
In the example above, I want to get the first error, which is "The name must be at least 5 characters.". How can I do that?
在上面的示例中,我想得到第一个错误,即“名称必须至少为 5 个字符。”。我怎样才能做到这一点?
回答by rome ?
Try this:
尝试这个:
if ($validator->fails()) {
$error = $validator->errors()->first();
}
回答by Valentine Shi
As per 2019 Laravel 5.8 it is as easy as this:
根据 2019 Laravel 5.8,它就像这样简单:
// create the validator and make a validation here...
if ($validator->fails()) {
$fieldsWithErrorMessagesArray = $validator->messages()->get('*');
}
You will get the array of arrays of the fields' names and error messages. Something like this:
您将获得字段名称和错误消息的数组数组。像这样的东西:
[
'price'=>
[
0 => 'Price must be integer',
1 => 'Price must be greater than 0'
]
'password' => [
[
0 => 'Password is required'
]
]
]
You can use other validation messages getters that Illuminate\Support\MessageBag
class provides (it is actually the object type that $validator->messages()
above returns).
您可以使用Illuminate\Support\MessageBag
该类提供的其他验证消息 getter (它实际上是$validator->messages()
上面返回的对象类型)。
Go to your_laravel_project_dir/vendor/illuminate/support/MessageBag.php
and find some useful methods like keys
, has
, hasAny
, first
, all
, isEmpty
etc. that you may need while checking for particular validation errors and customizing HTTP response messages.
去your_laravel_project_dir/vendor/illuminate/support/MessageBag.php
找到像一些有用的方法keys
,has
,hasAny
,first
,all
,isEmpty
等,你可以同时检查特定的验证错误和自定义HTTP响应消息的需要。
It is easy to understand what they do by the look at the source code. Here is the Laravel 5.8 API referencethough probably less useful than the source code.
通过查看源代码很容易理解它们的作用。这是Laravel 5.8 API 参考,虽然可能不如源代码有用。
回答by Jilson Thomas
In your ajax request, when you get the data
, try data.name
.
在您的Ajax请求,当你得到了data
,试试data.name
。
This will give you the error message for the name
field.
这将为您提供该name
字段的错误消息。
$.ajax({
url: "/your-save-url",
type: "post",
data: serializedData,
success: function(data) { alert(data.name)}
});
回答by ArtisanBay
If validation fails, the withErrors
method can be used to flash the error messages to the session. This is an array and this method will automatically share $errors
with all views after redirection.
如果验证失败,该withErrors
方法可用于将错误消息闪现到会话中。这是一个数组,此方法将$errors
在重定向后自动与所有视图共享。
return redirect('register')->withErrors($validator, 'login');
The MessageBag
can be accessed using the instance from the $errors
variable:
的MessageBag
可以使用来自实例进行访问$errors
的变量:
{{ $errors->login->first('email') }}
Form API docs
表单 API文档
Hope this is helpful.
希望这是有帮助的。
回答by Sinan Eldem
If you are using toastr style error displaying, this will work:
如果您使用 toastr 样式错误显示,这将起作用:
@if(session()->get('errors'))
toastr.error("{{ session()->get('errors')->first() }}");
@endif
回答by Julio Popócatl
if you want to do it inside the controller you can:
如果您想在控制器内执行此操作,您可以:
Arr::first(Arr::flatten($validator->messages()->get('*')))
you will get the first text message
你会收到第一条短信
The email must be accepted.