Laravel 5.2 验证检查值是否不等于变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34842487/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:03:26  来源:igfitidea点击:

Laravel 5.2 validation check if value is not equal to a variable

phpvalidationlaravellaravel-5.2

提问by patricus

In my instance one user is inviting another I would like to check if the user they are inviting is not themselves.

在我的例子中,一个用户正在邀请另一个用户,我想检查他们邀请的用户是否不是他们自己。

Thus I have two variables incomming emailand user->email

因此我有两个变量传入电子邮件用户->电子邮件

$this->validate($request, [
            'email' => 'required|email',
        ]);

How can I add that validation rule to the validation call?

如何将该验证规则添加到验证调用中?

回答by patricus

You can use not_in, which allows you to specify a list of values to reject:

您可以使用not_in,它允许您指定要拒绝的值列表:

$this->validate($request, [
    'email' => 'required|email|not_in:'.$user->email,
]);

回答by Hamed Yarandi

You can use different:fieldaccording to the laravel Document

可以different:field根据laravel文档使用

For instanse in your requests validation:

例如在您的请求验证中:

public function rules()
    {
        return [
            'from' => 'required',
            'to' => 'required|different:from',
            'action' => 'required',
            'access' => 'required'
        ];
    }

These two fromand toshould be different(not same).

这两个fromto应该不同(不相同)。