R
R
Ruslan Absalyamov2019-02-24 11:43:01
Yii
Ruslan Absalyamov, 2019-02-24 11:43:01

Why is my username missing when registering a user?

You need to add a user and in order not to write a lot, I used the standard bologer.ru/yii2-basic-registraciya-i-avtorizaciya-...
It works fine in registration, but when creating a user, somehow it’s not very
The form itself

<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
    <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
    <?= $form->field($model, 'email') ?>
    <?= $form->field($model, 'password')->passwordInput() ?>
    <div class="form-group">
        <?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
    </div>
    <?php ActiveForm::end(); ?>

in my controller
use app\models\SignupForm;
...
public function actionCreate()
    {
        $model = new SignupForm();

        if ($model->load(Yii::$app->request->post())) {
            if ($user = $model->signup()) {
                return $this->redirect(['index']);
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

And the SignupForm model
<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * Signup form
 */
class SignupForm extends Model
{

    public $username;
    public $email;
    public $password;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            ['username', 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This username has already been taken.'],
            ['username', 'string', 'min' => 2, 'max' => 255],
            ['email', 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'This email address has already been taken.'],
            ['password', 'required'],
            ['password', 'string', 'min' => 6],
        ];
    }

    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {

        if (!$this->validate()) {
            return null;
        }

        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        return $user->save() ? $user : null;
    }

}

Even if you do var_dump($user->username)
Then, it shows me the value and it is not empty, but in save it is not there at all, except for password auth_key and so on. What could be the catch here?
The value that outputs $user before
save
object(app\models\User)[137]
  public 'id' => null
  public 'username' => string 'rusline2' (length=8)
  public 'email' => string '[email protected]' (length=13)
  public 'created_at' => null
  public 'auth_date' => null
  private '_attributes' (yii\db\BaseActiveRecord) => 
    array (size=2)
      'password_hash' => string '$2y$13$g5S7bap.Zld602P9MDpY7.golGzMIGEsmSdBgUg5YV6b9zZ8PsxE6' (length=60)
      'auth_key' => string 'ofJNRZmje1Fbe-oJedDNlmIBa-233OP7' (length=32)
  private '_oldAttributes' (yii\db\BaseActiveRecord) => null
  private '_related' (yii\db\BaseActiveRecord) => 
    array (size=0)
      empty
  private '_relationsDependencies' (yii\db\BaseActiveRecord) => 
    array (size=0)
      empty
  private '_errors' (yii\base\Model) => null
  private '_validators' (yii\base\Model) => null
  private '_scenario' (yii\base\Model) => string 'default' (length=7)
  private '_events' (yii\base\Component) => 
    array (size=2)
      'beforeInsert' => 
        array (size=1)
          0 => 
            array (size=2)
              ...
      'beforeUpdate' => 
        array (size=1)
          0 => 
            array (size=2)
              ...
  private '_eventWildcards' (yii\base\Component) => 
    array (size=0)
      empty
  private '_behaviors' (yii\base\Component) => 
    array (size=1)
      0 => 
        object(yii\behaviors\TimestampBehavior)[136]
          public 'createdAtAttribute' => string 'created_at' (length=10)
          public 'updatedAtAttribute' => string 'updated_at' (length=10)
          public 'value' => null
          public 'attributes' => 
            array (size=2)
              ...
          public 'skipUpdateOnClean' => boolean true
          public 'preserveNonEmptyValues' => boolean false
          public 'owner' => 
            &object(app\models\User)[137]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question