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

爷,独闯天下 提交于 2019-12-23 07:39:16

问题


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 email and user->email

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

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


回答1:


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

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



回答2:


You can use different:field according to the laravel Document

For instanse in your requests validation:

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

These two from and to should be different(not same).



来源:https://stackoverflow.com/questions/34842487/laravel-5-2-validation-check-if-value-is-not-equal-to-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!