Laravel Illuminate\Validation\Rule 未找到
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40586716/
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 Illuminate\Validation\Rule not found
提问by hatemjapo
I am trying to submit a form and validate the content. In one of the requests I need to make a special rule. I followed the documentation and it says to use unique and declare a Rule.
我正在尝试提交表单并验证内容。在其中一个请求中,我需要制定一项特殊规则。我遵循了文档,它说要使用唯一性并声明规则。
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
I am trying with the example from the documentation, but all I get it this error:
我正在尝试使用文档中的示例,但我得到的只是这个错误:
Class 'Illuminate\Validation\Rule' not found
I declared the line
我声明了这条线
use Illuminate\Validation\Rule;
In my controller, but the error is still there.
在我的控制器中,但错误仍然存在。
回答by henrik
You dont have to use the Rule class for this.
您不必为此使用 Rule 类。
Simply achieve the same with following rule:
只需使用以下规则即可实现相同的目标:
'email' => 'required|unique:users,email,' . $user->id
回答by Programador Adagal
The Rule class in the example you posted is for validate an unique field. For examplo if you have an email you will want to be unique in the table, when you are going to edit the record, at saving, will get a validator error because you are saving the same email it is already in the db.
您发布的示例中的 Rule 类用于验证唯一字段。例如,如果您有一封电子邮件,您希望在表中是唯一的,当您要编辑记录时,在保存时,将收到验证器错误,因为您正在保存它已经在数据库中的同一电子邮件。
The example you posted:
您发布的示例:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
is related to this case, editing a record and validating the email (unique in table users). This example avoid to validate the email against the same user-
与这种情况有关,编辑记录并验证电子邮件(表用户中唯一的)。此示例避免针对同一用户验证电子邮件-
For using it you have to add the class (not included in the laravel installator). Hereyou have it.
要使用它,您必须添加类(不包含在 laravel 安装程序中)。给你。
In your question you say about using the unique rule. The unique rule is for fields that has to be unique in a table, an email, an personal identification (law), and more. Verify if the unique rule is what you need.
在您的问题中,您提到了使用唯一规则。唯一规则适用于必须在表、电子邮件、个人身份(法律)等中唯一的字段。验证唯一规则是否是您所需要的。
回答by felipsmartins
The Illuminate\Validation\Rule
class has been added on Laravel 5.3+.
I almot sure you have tested with an old laravel version.
该Illuminate\Validation\Rule
班已经添加到Laravel 5.3+。
我非常确定您已经使用旧的 Laravel 版本进行了测试。
See issue (PR) 15809: [5.3] Object based unique and exists rules for validation.
请参阅问题 (PR) 15809:[5.3] 基于对象的唯一和存在的验证规则。
回答by Samuel Martins
I had that problem on version v5.3
. I just updated to the latest patch version (v5.3.31
in my case) and all worked correctly!
我在版本上遇到了这个问题v5.3
。我刚刚更新到最新的补丁版本(v5.3.31
就我而言)并且一切正常!
回答by Félix Urrutia
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users')->ignore($user->id),
],
]);
You have done everything ok so far until you add the line
"Rule::unique('users')->ignore($user->id)
"
到目前为止,您已经完成了一切,直到您添加了“ Rule::unique('users')->ignore($user->id)
”行
The only reason why this is not working is because you are not specifying the column name you want to be unique. Let's assume that, in your database, the column name of the table users that you want to be unique is "email". Then, your code should be as follows:
这不起作用的唯一原因是您没有指定要唯一的列名。假设在您的数据库中,您希望唯一的表 users 的列名称是“email”。然后,您的代码应如下所示:
use Illuminate\Validation\Rule;
Validator::make($data, [
'email' => [
'required',
Rule::unique('users', 'email')->ignore($user->id),
],
]);
Thus,
因此,
//Right Way
"Rule::unique('users', 'email')->ignore($user->id)"
//Wrong Way
"Rule::unique('users')->ignore($user->id)"
回答by Morpheus_ro
Why don't you try in you method something like this:
你为什么不试试你的方法是这样的:
$validator = Validator::make($request->all(), [
'email' => 'required|unique:<table_name>',
]);
if ($validator->fails()) {
...
}
If you column if also named email laravel will know automaticly, else do
如果你列 if 也命名为 email laravel 会自动知道,否则做
required|unique:<table_name>,<column_name>
Here is a link to the documentation validation section: https://laravel.com/docs/5.3/validation
这是文档验证部分的链接:https: //laravel.com/docs/5.3/validation