在 Laravel 中提交表单之前确认密码


Confirm the password before a form submit in Laravel

我正在使用Laravel 4.2,我想在使用类似模态的东西提交表单之前确认用户的密码,而无需进行实际提交。如果密码匹配,则进行提交;如果没有,请保持在同一页面而不进行重新加载,可以吗?

我该怎么做?

模型中添加以下内容:

public function afterValidate() {
        if (count($this->errors()->toArray())==0 && !empty($this->password) && $this->password!=$this->getOriginal('password')) {
            $this->password = Hash::make($this->password);  // encrypting the password
            unset($this->password_confirmation);    // dropping password confirmation field
        }
    }

这在您的规则中:

'password' => 'Required|alpha_num|between:6,12|confirmed',
'password_confirmation' => 'Required|alpha_num|between:6,12|same:password',

看看,如果有帮助。

获取提交按钮 ID,用您自己的代码覆盖 onclick(),调出确认密码的模式,并返回 false,以便表单实际上不会被提交。当用户在您的模态上单击"确定"时,如果密码匹配,则从JS提交表单。