Yii2:比较密码验证和补救措施

时间:2015-07-30 17:08:21

标签: php yii2 yii2-basic-app yii2-validation

我是Yii的初学者,这就是为什么无法弄清楚,错误的原因。 我使用相同的模型 - 用户 - 用于登录,注册和版本配置文件。

用户模型

class Users extends \yii\db\ActiveRecord {
    public $password_repeat; //have no this value in DB, that's why write it as property directly. Need only for profile editing

    public function rules() {
        return [
            [['username', 'password', 'authKey', 'accessToken'], 'safe'],
            [['username', 'password'], 'required'],
            [['username', 'password', 'authKey', 'accessToken'], 'string', 'max' => 255],
            [['is_admin'], 'boolean'],

//don't use scenarios() here. Use 'on' instead 
            ['password_repeat', 'required', 'on' => 'update'],
            ['password_repeat', 'compare', 'compareAttribute'=>'password', 'message'=>"Passwords don't match", 'on' => 'update' ],        
        ];
    }
}

查看摘要

<?php $form = ActiveForm::begin([
    'id' => 'data-form',
    'options' => ['class' => 'form-horizontal', 'enctype' => 'multipart/form-data'],
    'fieldConfig' => [
        'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-6\">{error}</div>",
        'labelOptions' => ['class' => 'col-lg-1 control-label'],
    ],
]); ?>

<?= $form->field($user_data, 'username') ?>

<?= $form->field($logo_data, 'imageFile')->fileInput() ?>

 <?= $form->field($user_data, 'password')->passwordInput(['value'=>''])?>
 <?= $form->field($user_data, 'password_repeat')->passwordInput(['value'=>''])?>

 <?= Html::submitButton('Zapisz', ['class' => 'btn btn-primary btn-block', 'name' => 'data-button']) ?>

<?php ActiveForm::end(); ?>

网站控制器操作

public function actionProfile(){
    //3 model instances to retrieve data from users && company_profiles and logo
    $user_data = Users::find()->where(['id'=>Yii::$app->user->id])->one();
    $company_profile_data = CompanyProfiles::find()->where(['user_id'=>Yii::$app->user->id])->one();
    $logo_data = new UploadLogo();

    //here I upload file through $logo_data

    if (isset($_POST['Users'])){
        $user_data->username = $_POST['Users']['username'];
        $company_profile_data->firm_data = $_POST['CompanyProfiles']['firm_data'];
        //other assignments

        $user_data->scenario = 'update'; //specially for 'profile' page           

        if (($_POST[Users][password] !== '') && ($_POST[Users][password_repeat]) !== '' && ($_POST[Users][password] == $_POST[Users][password_repeat]))
           $user_data->password_repeat = $user_data->password = md5($_POST[Users][password]); //MAYBE, problem is here?

        $user_data->update();
        $company_profile_data->update();
    }

    return $this->render('profile', ['user_data' => $user_data, 'company_profile_data' => $company_profile_data, 'logo_data' => $logo_data]);
}

我使用更新方案进行个人资料编辑页面。在此方案期间,password_repeat是必需的,并且需要与密码相同。但它并没有像预期的那样发挥作用。 A:当我尝试在没有输入密码的情况下提交表单并重新提交时,我看到消息&#34;不能为空白&#34; 仅用于密码。然后我把值放在ONLY传输输入和页面重新加载(数据库表没有改变),重新加载后我看到消息&#34;不能为空白&#34; 这次是为了补偿领域!但最奇怪的是,在下一页重新加载之前一切正常。重新加载后转到A; :) 如果我删除方案,一切都可以通过验证。但是我无法删除它,因为在这种情况下注册失败(需要重新填写字段)。

那么,我需要使用pass-repass功能才能正常工作(使用场景&#39;更新&#39;)?错误在哪里?

1 个答案:

答案 0 :(得分:2)

知道了!我只需要移动这个代码行:

$user_data->scenario = 'update';

来自 IF stmt。现在我想养成在创建模型实例时直接分配场景属性的习惯。