how to remove username?
Opened this issue · 1 comments
i removed the username in my view.
in my registrationForm.php this is my rules.
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernameTrim' => ['username', 'filter', 'filter' => 'trim'],
'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'],
'usernameRequired' => ['username', 'default'], // https://yii2-framework.readthedocs.io/en/stable/guide/input-validation/#handling-empty-inputs
'usernameUnique' => [
'username',
'unique',
'targetClass' => \app\models\user\Model\User::class, //$user,
'message' => Yii::t('usuario', 'This username has already been taken'),
],
but i still get an error "User could not be registered. " when i enable it in my view and put in a random username it works fine. but when username is empty i get "user could not be registered" how do i remove username?
username field is required at database level too (NOT NULL and Uniquie Index), so it would also require you to change the database structure for this to work.
IMHO you should simply set the username the same value of the email field.
You could simply use the "default" validator to copy the value, something like:
[['username'], 'default', 'value' => function ($model, $attribute) {
return $model->email;
}],
this though would NOT work correctly when the e-mail is changed.
A better solution would be to override the User model and set the username (with the e-mail value or a random string) in the beforeSave()