Confirm the password before a form submit in Laravel

冷暖自知 提交于 2020-01-26 02:05:12

问题


I am using Laravel 4.2 and I want to confirm the user's password before a form submit with something like a modal, without doing the actual submit. If the password matches, do the submit; if not, keep in the same page without doing a reload, it is possible?

How can I do that?


回答1:


Add this in your model:

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
        }
    }

This in your rules:

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

See, if that helps.




回答2:


Get the submit button ID, override the onclick() with your own code, bring up your modal that confirms the password, and return false so the form doesn't actually get submitted. When the user clicks OK on your modal, if the password matches then do a form submit from JS.



来源:https://stackoverflow.com/questions/27873431/confirm-the-password-before-a-form-submit-in-laravel

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