Why base drops an empty value?

There is a form, it should send the data to the database.
View:
<?php $form = ActiveForm::begin();
?>
 <?= $form--->field($registrationModel, 'login'); ?>
 <?= $form--->field($registrationModel, 'password')->passwordInput(); ?>
 <?= $form--->field($registrationModel, 'role')->dropDownList(['manager' => 'Manager', 'user' => 'Artist']); ?>
 <div class="form-group">
 <div class>
 <?= Html::submitButton('Create', ['class' =--> 'btn btn-primary']); ?>
</div>
</div>
 <?php ActiveForm::end(); ?>

Model:
<?php

namespace app\models;


use yii\db\ActiveRecord;


class RegistrationModel extends ActiveRecord
{
 public $login;
 public $password;
 public $role;

 public static function tableName()
{
 return 'users';
}

 public function rules()
{
 return [
 // username and password are both required
 [['login', 'password', 'role'], 'required'],
];
}

 public function attributeLabels()
{
 return [
 'login' =--> 'Login',
 'password' => 'Password',
 'role' => 'user Role',
];
}

 public function createAccount()
{
 $postData = $_POST['RegistrationModel'];
 $this->login = $postData['login'];
 $this->password = $postData['password'];
 $this->role = $postData['role'];
$this->save();
}

}


Controller:
<?php

namespace app\controllers;


use app\models\RegistrationModel;
use yii\web\Controller;
use Yii;

class RegistrationController extends Controller
{

 public function actionView()
{

 $registrationModel = new RegistrationModel();
 if( $registrationModel--->load( Yii::$app->request->post() ) && $registrationModel->createAccount() ){
 Yii::$app->session->getFlash('successRegistration', 'User created successfully');
}
 return $this->render('view', compact(['registrationModel', 'postData']));
}

}

I've tried to write just a line instead of $_POST, it is still recorded in the database only the default value and selects
June 10th 19 at 16:27
1 answer
June 10th 19 at 16:29
Solution
Good evening.
Because here it is necessary to remove.
public function createAccount()
{
 $postData = $_POST['RegistrationModel'];
 $this->login = $postData['login'];
 $this->password = $postData['password'];
 $this->role = $postData['role'];
$this->save();
 }

And to write:
public function actionCreate()
{
 $registrationModel = new RegistrationModel();
 if( $registrationModel->load( Yii::$app->request->post() ) && $registrationModel->save() ){
 Yii::$app->session->getFlash('successRegistration', 'User created successfully');
 return $this->redirect(['view', 'id' => $registrationModel->id])
}
 return $this->render('create', ['registrationModel' => $registrationModel]);
 }

But this is only just a entry in database. If You want to do normal check - have to redo everything.
Thanks for the reply.
Tried that, tried all in one view to do, to make a method in controller to write a normal string in $this->login, still retains the empty field and select a first value.
About to redo it all will not prompt, where you can read/watch something decent on yii2? - kimberly.Predovic commented on June 10th 19 at 16:32
show the model. - Dion77 commented on June 10th 19 at 16:35
,
<?php

namespace app\models;


use yii\db\ActiveRecord;


class RegistrationModel extends ActiveRecord
{
 public $login;
 public $password;
 public $role;

 public static function tableName()
{
 return 'users';
}

 public function rules()
{
 return [
 [['login', 'password', 'role'], 'required'],
];
}

 public function attributeLabels()
{
 return [
 'login' =--> 'Login',
 'password' => 'Password',
 'role' => 'user Role',
];
}

}
- kimberly.Predovic commented on June 10th 19 at 16:38
something worthy of yii2, from the developer:
Times(in Russian)
Two(the same bourgeois)
Websites and channels from good people:
Times(see tagged yii2)
Two - Dion77 commented on June 10th 19 at 16:41
is all the model? In the table which fields? - Dion77 commented on June 10th 19 at 16:44
fully
fields - id(ai), login, password, role(enum - manager user) - kimberly.Predovic commented on June 10th 19 at 16:47
that you model this:
public $login;
 public $password;
 public $role;

In the end, the model should be:
<?php

namespace app\models;


use yii\db\ActiveRecord;


class RegistrationModel extends ActiveRecord
{
 public static function tableName()
{
 return 'users';
}

 public function rules()
{
 return [
 // username and password are both required
 [['login', 'password', 'role'], 'required'],
];
}

 public function attributeLabels()
{
 return [
 'login' =--> 'Login',
 'password' => 'Password',
 'role' => 'user Role',
];
}
}

And the controller:
public function actionCreate()
{
 $registrationModel = new RegistrationModel();
 if( $registrationModel->load( Yii::$app->request->post() ) && $registrationModel->save() ){
 Yii::$app->session->getFlash('successRegistration', 'User created successfully');
 return $this->redirect(['view', 'id' => $registrationModel->id])
}
 return $this->render('create', ['registrationModel' => $registrationModel]);
 }
- Dion77 commented on June 10th 19 at 16:50

Find more questions by tags Yii