简体   繁体   English

Laravel 5.5表单已通过密码确认

[英]Laravel 5.5 form confirmed by password

Im making form via LaravelCollective forms in my app. 我通过我的应用程序中的LaravelCollective表单制作表单。 I have email changing form with fields: New e-mail Confirm Email Password 我有以下字段的电子邮件更改表格:新电子邮件确认电子邮件密码

I need to validate user password on this form with user password in database. 我需要使用数据库中的用户密码来验证此表单上的用户密码。 Can i do it via Validate? 我可以通过验证吗?

My form: 我的表格:

{{ Form::model($user, ['route' => ['profile.email.update']]) }}
<div class="form-group row">
    <div class="col-md-12">
        <h2>Edytuj email</h2>
        <div class="form-group">
            <label for="edit-page-email">Nowy adres email</label>

            {{Form::text('email',null,['class' => 'form-control', 'id'=>'edit-page-email'])}}
        </div>
        <div class="form-group">
            <label for="edit-page-repemail">Nowy adres email (powtórz)</label>
            {{Form::text('email_confirmation',null,['class' => 'form-control', 'id'=>'edit-page-email'])}}
        </div>
        <div class="form-group">
            <label for="edit-page-pass">Twoje hasło</label>
                {{Form::password('password',['class' => 'form-control', 'id'=>'edit-page-npass'])}}
        </div>
        <div class="col-md-12 text-center">
                {{Form::submit('Zapisz zmiany',['class' => 'btn btn-primary btm-sm']) }}
            <a href="#" class="btn btn-link btn-sm font-red" title="Anuluj">Anuluj</a>
        </div>
    </div>
</div>
{{Form::close()}}

And validation is: 验证是:

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

Any ideas how can i do this? 任何想法我该怎么做?

Not sure when you say I need to validate user password on this form with user password in database 不知道何时您说我需要使用数据库中的用户密码来验证此表单上的用户密码

However the password and password confirm validation should look like the following 但是,密码和密码确认验证应如下所示

{{ Form::password('password',['class'=> 'form-control','placeholder'=>'Enter your Password']) }}
{{ Form::password('password_confirmation', ['class' => 'form-control','placeholder'=>'Re-enter your Password']) }}

Now your validation rules should be 现在您的验证规则应为

'password'   => 'required|confirmed',

No you cannot do this with built in validation rules of Laravel. 不,您不能使用Laravel的内置验证规则来执行此操作。 Beside, you might already know that Laravel store password using bycrypt method. 另外,您可能已经知道Laravel使用bycrypt方法存储密码。 This is not just a plain string comparison operation too. 这也不只是一个简单的字符串比较操作。

So, you need to write your own custom validation to check password, or you can just check password and return error if not matched instead of validation. 因此,您需要编写自己的自定义验证来检查密码,或者您可以只检查密码并返回错误(如果不匹配)而不是验证。

Note: The confirmed validation rule is to check one password is matched with another password filed or not. 注意:确认的确认规则是检查一个密码与另一个密码是否匹配。 This is basically used to match two password form in single form. 这基本上是用来匹配单个形式的两个密码形式。 Not to check associated hashed password with the email in db. 不检查与数据库中的电子邮件关联的哈希密码。

You can compare a string with the user's stored password using this 您可以使用此方法将字符串与用户存储的密码进行比较

$pwd = "secret";
$user = User::find(1);
Hash::check($pwd, $user->password); //returns a boolean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM