Laravel 相同的验证器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28081967/
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 same validator
提问by Nivin V Joseph
My case is for change password option. I already have current password in object $pass
. I want to validate this $pass
against textbox form input current_password to proceed to create a new password for the user. How to validate with same validator. Sorry I'm new to laravel.
我的情况是更改密码选项。我已经在 object 中有当前密码$pass
。我想$pass
根据文本框表单输入 current_password验证这一点,以继续为用户创建新密码。如何使用相同的验证器进行验证。抱歉,我是 Laravel 的新手。
$rules = array('password_current' => "required|same:$pass");
doesn't work.
不起作用。
回答by Dark Cyber
since same:
used to ensure that the value of current field is the same as another fielddefined by the rule parameter (not object). so you can't use this function take a look this example code below.
因为same:
用于确保当前字段的值与规则参数(而不是对象)定义的另一个字段相同。所以你不能使用这个函数看看下面的这个示例代码。
$data = Input::all();
$rules = array(
'email' => 'required|same:old_email',
);
the above code will check if current email field is same as old_email field. so i think you can you simple if else
上面的代码将检查当前电子邮件字段是否与 old_email 字段相同。所以我想你能简单点吗
in your handle controller function assume
在您的手柄控制器功能中假设
public function handleCheck(){
$current_password = Input::get('current_password');
$pass = //your object pass;
if($current_password == $pass){
// password correct , show change password form
}else{
// password incorrect , show error
}
}
let me know if it works. see Laravel Validation same
让我知道它是否有效。参见Laravel 验证相同
回答by Shan
If you have password stored in $pass
already just inject $pass in the request and use its field instead for e.g.
如果您已经存储了密码,$pass
只需在请求中注入 $pass 并使用其字段代替例如
$request->request->add(['password_old' => $pass]);
Then, you can validate it like
然后,您可以像这样验证它
$rules = array('password_current' => "required|same:password_old");