Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Deltwin opened this issue Aug 15, 2023 · 2 comments
Open

Comments

@Deltwin
Copy link

Deltwin commented Aug 15, 2023

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

@Deltwin Deltwin changed the title Getting unknown property: app\models\User::banned_at Getting unknown property: amnah\yii2\user\models\forms\LoginForm::username Aug 15, 2023
@amnah
Copy link
Owner

amnah commented Aug 16, 2023

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
Copy link
Author

Deltwin commented Aug 16, 2023

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 (models/User.php)
  • account.php (views/user/account.php)
  • login.php (views/user/login.php)
  • register.php (views/user/register.php)

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;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants