amnah / yii2-user

Yii2 user authentication module
MIT License
253 stars 104 forks source link

Getting unknown property: amnah\yii2\user\models\forms\LoginForm::username #222

Open Deltwin opened 1 year ago

Deltwin commented 1 year ago

Getting: Unknown property: amnah\yii2\user\models\forms\LoginForm::username with latest version (5.0.9).

amnah commented 1 year ago

Where did you get username from? The field should be called email

https://github.com/amnah/yii2-user/blob/master/models/forms/LoginForm.php#L16

Deltwin commented 1 year ago

Oh, thanks for your reply! I'm updating the code because the real owner of my repository has last updated 9 years ago his repository and he has username and email in his repository as field. Also, I forgot to say I'm using PHP >8.0 (But it's working perfectly except for this problem :P)

If you wanna check the code GitHub It would be an honour :)

The files that have 'username' inside are:

User.php:

class User extends BaseUser
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        // set initial rules
        $rules = [
            // general email and username rules
            [['email', 'username'], 'string', 'max' => 255],
            [['email', 'username'], 'unique'],
            [['email', 'username'], 'filter', 'filter' => 'trim'],
            [['email'], 'email'],
            [['username'], 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message' => Yii::t('user', '{attribute} can contain only letters, numbers, and "_"')],

            // password rules
            [['newPassword'], 'string', 'min' => 3],
            [['newPassword'], 'filter', 'filter' => 'trim'],
            [['newPassword'], 'required', 'on' => ['register', 'reset', 'socialonlyaccount']],
            [['newPasswordConfirm'], 'required', 'on' => ['reset', 'socialonlyaccount']],
            [['newPasswordConfirm'], 'compare', 'compareAttribute' => 'newPassword', 'message' => Yii::t('user','Passwords do not match')],

            // account page
            [['currentPassword'], 'required', 'on' => ['account']],
            [['currentPassword'], 'validateCurrentPassword', 'on' => ['account']],

            // admin crud rules
            [['role_id', 'status'], 'required', 'on' => ['admin']],
            [['role_id', 'status'], 'integer', 'on' => ['admin']],
            [['banned_at'], 'integer', 'on' => ['admin']],
            [['banned_reason'], 'string', 'max' => 255, 'on' => 'admin'],
        ];

        // add required rules for email/username depending on module properties
        $requireFields = ["requireEmail", "requireUsername"];
        foreach ($requireFields as $requireField) {
            if (Yii::$app->getModule("user")->$requireField) {
                $attribute = strtolower(substr($requireField, 7)); // "email" or "username"
                $rules[]   = [$attribute, "required"];
            }
        }

        return $rules;
    }
    public function attributeLabels()
    {
        return [
            'username' => Yii::t('app', 'Username'),
            'newPassword' => Yii::t('app', 'New Password'),
            'currentPassword' => Yii::t('app', 'Current Password'),
        ];
    }    
}

Account.php:

<?php if (Yii::$app->getModule("user")->useUsername): ?>
        <?= $form->field($user, 'username') ?>
<?php endif; ?>

Login.php:

<?= $form->field($model, 'username')->label(false,['class'=>'label-class'])->textInput(array('placeholder' => Yii::t('app', 'E-mail or Username'))) ?>

Register.php:

 <?php if (Yii::$app->getModule("user")->requireUsername): ?>
            <?= $form->field($user, 'username') ?>
        <?php endif; ?>

If you need to know how the DB is setted-up:

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `role_id` int(11) NOT NULL,
  `status` smallint(6) NOT NULL,
  `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `new_email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `username` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `password` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `auth_key` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `api_key` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `login_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `login_time` timestamp NULL DEFAULT NULL,
  `create_ip` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  `banned_at` timestamp NULL DEFAULT NULL,
  `banned_reason` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tb_user_email` (`email`) USING BTREE,
  UNIQUE KEY `tb_user_username` (`username`) USING BTREE,
  KEY `tb_user_role_id` (`role_id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3654 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;