i have signup form register new user. when input data fields exept one(username) saving in db. can't figure why. me.
here code signupform
<?php namespace app\modules\user\models; use yii\base\model; use yii; /** * signup form */ class signupform extends model { public $username; public $email; public $password; public $verifycode; public function rules() { return [ ['username', 'filter', 'filter' => 'trim'], ['username', 'required'], ['username', 'match', 'pattern' => '#^[\w_-]+$#i'], ['username', 'unique', 'targetclass' => user::classname(), 'message' => 'this username has been taken.'], ['username', 'string', 'min' => 2, 'max' => 255], ['email', 'filter', 'filter' => 'trim'], ['email', 'required'], ['email', 'email'], ['email', 'unique', 'targetclass' => user::classname(), 'message' => 'this email address has been taken.'], ['password', 'required'], ['password', 'string', 'min' => 6], ['verifycode', 'captcha', 'captchaaction' => '/user/default/captcha'], ]; } public function attributelabels() { return [ 'id' => 'id', 'username' => yii::t('app', 'user_username'), 'email' => yii::t('app', 'user_email'), 'password' => yii::t('app', 'user_password'), 'verifycode' => yii::t('app', 'user_verifycode'), ]; } /** * signs user up. * * @return user|null saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new user(); $user->username = $this->username; $user->email = $this->email; $user->setpassword($this->password); $user->status = user::status_wait; $user->generateauthkey(); $user->generateemailconfirmtoken(); if ($user->save()) { yii::$app->mailer->compose('@app/modules/user/mails/emailconfirm', ['user' => $user]) ->setfrom([yii::$app->params['supportemail'] => yii::$app->name]) ->setto($this->email) ->setsubject('email confirmation ' . yii::$app->name) ->send(); } return $user; } return null; } }
code signup(view)
<?php use yii\captcha\captcha; use yii\helpers\html; use yii\bootstrap\activeform; /* @var $this yii\web\view */ /* @var $form yii\bootstrap\activeform */ /* @var $model app\modules\user\models\signupform */ $this->title = yii::t('app', 'title_signup'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="user-default-signup"> <h1><?= yii::t('app', 'title_signup') ?></h1> <p>please fill out following fields signup:</p> <div class="row"> <div class="col-lg-5"> <?php $form = activeform::begin(['id' => 'form-signup']); ?> <?= $form->field($model, 'username') -> textinput() ?> <?= $form->field($model, 'email') -> textinput() ?> <?= $form->field($model, 'password')->passwordinput() ?> <?= $form->field($model, 'verifycode')->widget(captcha::classname(), [ 'captchaaction' => '/user/default/captcha', 'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', ]) ?> <div class="form-group"> <?= html::submitbutton('signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> </div> <?php activeform::end(); ?> </div> </div> </div>
code defaultcontroller
... public function actionsignup() { $model = new signupform(); if ($model->load(yii::$app->request->post())) { if ($user = $model->signup()) { yii::$app->getsession()->setflash('success', 'Подтвердите ваш электронный адрес.'); return $this->gohome(); } } return $this->render('signup', [ 'model' => $model, ]); } ...
code user(model)
<?php namespace app\modules\user\models; use yii; use yii\base\notsupportedexception; use yii\behaviors\timestampbehavior; use yii\db\activerecord; use yii\helpers\arrayhelper; use yii\web\identityinterface; /** * model class table "{{%user}}". * * @property integer $id * @property integer $created_at * @property integer $updated_at * @property string $username * @property string $auth_key * @property string $email_confirm_token * @property string $password_hash * @property string $password_reset_token * @property string $email * @property integer $status */ class user extends activerecord implements identityinterface { const scenario_profile = 'profile'; const status_blocked = 0; const status_active = 1; const status_wait = 2; public $id; public $username; public $password; public $authkey; public $accesstoken; public function rules() { return [ ['username', 'required'], ['username', 'match', 'pattern' => '#^[\w_-]+$#i'], ['username', 'unique', 'targetclass' => self::classname(), 'message' => 'this username has been taken.'], ['username', 'string', 'min' => 2, 'max' => 255], ['email', 'required', 'except' => self::scenario_profile], ['email', 'email', 'except' => self::scenario_profile], ['email', 'unique', 'targetclass' => self::classname(), 'except' => self::scenario_profile, 'message' => yii::t('app', 'error_email_exists')], ['email', 'string', 'max' => 255, 'except' => self::scenario_profile], ['status', 'integer'], ['status', 'default', 'value' => self::status_active], ['status', 'in', 'range' => array_keys(self::getstatusesarray())], ]; } public function attributelabels() { return [ 'id' => 'id', 'created_at' => yii::t('app', 'user_created'), //'Создан', 'updated_at' => yii::t('app', 'user_update'), //'Обновлён', 'username' => yii::t('app', 'user_username'), // 'Имя пользователя', 'email' => yii::t('app', 'user_email'), // 'email', 'status' => yii::t('app', 'user_status'), //'Статус', ]; } public function scenarios() { return [ self::scenario_default => ['username', 'email', 'status'], self::scenario_profile => ['email'], ]; } public function behaviors() { return [ timestampbehavior::classname(), ]; } /** * @inheritdoc */ public static function findidentity($id) { return static::findone(['id' => $id, 'status' => self::status_active]); } /** * @inheritdoc */ public static function findidentitybyaccesstoken($token, $type = null) { throw new notsupportedexception('findidentitybyaccesstoken not implemented.'); } /** * finds user username * * @param string $username * @return static|null */ public static function findbyusername($username) { return static::findone(['username' => $username]); } /** * @inheritdoc */ public function getid() { return $this->getprimarykey(); } /** * @inheritdoc */ public function getauthkey() { return $this->auth_key; } /** * @inheritdoc */ public function validateauthkey($authkey) { return $this->getauthkey() === $authkey; } /** * validates password * * @param string $password password validate * @return boolean if password provided valid current user */ public function validatepassword($password) { return yii::$app->security->validatepassword($password, $this->password_hash); } public function getstatusname() { return arrayhelper::getvalue(self::getstatusesarray(), $this->status); } public static function getstatusesarray() { return [ self::status_blocked => 'Заблокирован', self::status_active => 'Активен', self::status_wait => 'Ожидает подтверждения', ]; } /** * @param string $password */ public function setpassword($password) { $this->password_hash = yii::$app->security->generatepasswordhash($password); } /** * generates "remember me" authentication key */ public function generateauthkey() { $this->auth_key = yii::$app->security->generaterandomstring(); } public function beforesave($insert) { if (parent::beforesave($insert)) { if ($insert) { $this->generateauthkey(); } return true; } return false; } //************************************ /** * finds user password reset token * * @param string $token password reset token * @return static|null */ public static function findbypasswordresettoken($token) { if (!static::ispasswordresettokenvalid($token)) { return null; } return static::findone([ 'password_reset_token' => $token, 'status' => self::status_active, ]); } /** * finds out if password reset token valid * * @param string $token password reset token * @return boolean */ public static function ispasswordresettokenvalid($token) { if (empty($token)) { return false; } $expire = yii::$app->params['user.passwordresettokenexpire']; $parts = explode('_', $token); $timestamp = (int) end($parts); return $timestamp + $expire >= time(); } /** * generates new password reset token */ public function generatepasswordresettoken() { $this->password_reset_token = yii::$app->security->generaterandomstring() . '_' . time(); } /** * removes password reset token */ public function removepasswordresettoken() { $this->password_reset_token = null; } //************************************ /** * @param string $email_confirm_token * @return static|null */ public static function findbyemailconfirmtoken($email_confirm_token) { return static::findone(['email_confirm_token' => $email_confirm_token, 'status' => self::status_wait]); } /** * generates email confirmation token */ public function generateemailconfirmtoken() { $this->email_confirm_token = yii::$app->security->generaterandomstring(); } /** * removes email confirmation token */ public function removeemailconfirmtoken() { $this->email_confirm_token = null; } }
it's because have got username
attribute directly declared in user
model here:
public $username;
remove can mapped activerecord.
see the note in guide this.
Comments
Post a Comment