I'm new to yii2 and I want to make a signup form using yii2 basic I use the generated mvc from gii but when i submit the registration it returned bad request but some of the data i've input is in the database but some are missing too. Here is my user model:
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user".
*
* @property int $id
* @property string $username
* @property string $name
* @property string $password
* @property string $authKey
* @property int $id_level
* @property string $accessToken
*
* @property MasterLevel $level
*/
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'name', 'password', 'authKey', 'id_level', 'accessToken'], 'required'],
[['id_level'], 'integer'],
[['username'], 'string', 'max' => 30],
[['name'], 'string', 'max' => 50],
[['password', 'authKey'], 'string', 'max' => 20],
[['accessToken'], 'string', 'max' => 10],
[['id_level'], 'exist', 'skipOnError' => true, 'targetClass' => MasterLevel::className(), 'targetAttribute' => ['id_level' => 'id_level']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'name' => 'Name',
'password' => 'Password',
'authKey' => 'Auth Key',
'id_level' => 'Id Level',
'accessToken' => 'Access Token',
];
}
/**
* Gets query for [[Level]].
*
* @return \yii\db\ActiveQuery
*/
public function getLevel()
{
return $this->hasOne(MasterLevel::className(), ['id_level' => 'id_level']);
}
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
/**
* {@inheritdoc}
*/
public static function findIdentity($id)
{
return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* {@inheritdoc}
*/
public static function findIdentityByAccessToken($token, $type = null)
{
foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
return null;
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
foreach (self::$users as $user) {
if (strcasecmp($user['username'], $username) === 0) {
return new static($user);
}
}
return null;
}
/**
* {@inheritdoc}
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* {@inheritdoc}
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}
And my actionCreate in user controller
public function actionCreate()
{
$model = new User();
if ($model->load(Yii::$app->request->post()) && $model->save(false)) {
return $this->redirect(['view', 'id' => $model->id]);
}
return $this->render('create', [
'model' => $model,
]);
}
the view
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model app\models\User */
$this->title = 'Create User';
$this->params['breadcrumbs'][] = ['label' => 'Users', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="user-create">
<h1><?= Html::encode($this->title) ?></h1>
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
I can't find where my code is wrong Please help me
save(false)
method does not validate your data!
That's why some data may not be included in the database.
Consider the following:
1- The data of your rules()
method must match the database.(rules() In model file)
2- Use the following in action:
if ($model->load(\Yii::$app->request->post()) && $model->save()){
#Code ...
Or:
if ($model->load(Yii::$app->request->post()) && $model->validate()){
#Code ...
if ($model->save(false)){
#Code ...