问题
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