php - yii2 model validating but form not storing data in database -


when validate form $model->validate() validation works perfect when data filled in form still not saving data database , not redirecting. tell me solution.

below code

controller's code of actioncreate():

public function actioncreate() {     $model = new clientusers();      if($model->load(yii::$app->request->post())) {         // && $model->validate()         //get file instance         $imagename = $model->first_name . '_' . yii::$app->security->generaterandomstring();         if ($model->file = uploadedfile::getinstance($model, 'file'))         {             $model->profile_pic = '../../user_uploads/' . $imagename . '.' . $model->file->extension;             $model->file->saveas('../../user_uploads/' . $imagename . '.' . $model->file->extension);         }         $model->created_date = date('d-m-y h:m:s');         $model->updated_date = date('d-m-y h:m:s');         $model->updated_by = yii::$app->user->identity->username;         $model->password_hash = $model->setpassword($model->password_hash);         $model->auth_key = $model->generateauthkey();         //$model->validate();         $model->save();          return $this->redirect(['view', 'id' => $model->id]);       }         else {          return $this->render('create', [             'model' => $model,         ]);     } } 

model's code:

<?php namespace backend\models;  use yii;  use yii\base\model;  /**  * model class table "client_users".  *  * @property integer $id  * @property string $first_name  * @property string $last_name  * @property string $email  * @property string $mobile  * @property integer $gender  * @property string $profile_pic  * @property string $address  * @property integer $city  * @property integer $state  * @property integer $country  * @property integer $pincode  * @property string $status   * @property string $created_date  * @property string $updated_by  * @property string $updated_date  * @property string $username  * @property string $auth_key  * @property string $password_hash  * @property string $password_reset_token  */ class clientusers extends \yii\db\activerecord {     /**      * @inheritdoc      */     public $file;     public static function tablename()     {         return 'client_users';     }      /**      * @inheritdoc      */     public function rules()     {         return [             [['first_name', 'last_name', 'email', 'mobile', 'gender',/* 'profile_pic', */'address', 'city', 'state', 'country', 'pincode', 'created_date',/* 'updated_by', 'updated_date',*/ 'username', 'auth_key', 'password_hash',/* 'password_reset_token'*/], 'required'],             [['gender', 'city', 'state', 'country', 'pincode'], 'integer'],             [['gender', 'city', 'state', 'country', 'pincode'], 'required'],             [['first_name', 'last_name', 'email',  'created_date', 'updated_by', 'updated_date'], 'string', 'max' => 50],             [['mobile'], 'string', 'max' => 20],             [['address','profile_pic'], 'string', 'max' => 300],             [['status'], 'string', 'max' => 10],             [['status'],'required'],             [['email'],'email'],             [['username', 'password_hash', 'password_reset_token'], 'string', 'max' => 255],             [['auth_key'], 'string', 'max' => 32],             [['file'],'safe'],             [['file'],'file', 'skiponempty' => true],             ['email', 'unique', 'targetclass' => '\common\models\user', 'message' => 'this email address has been taken.'],             ['username', 'unique', 'targetclass' => '\common\models\user', 'message' => 'this username has been taken.'],          ];     }       /**      * @inheritdoc      */     public function attributelabels()     {         return [             'id' => 'id',             'first_name' => 'first name',             'last_name' => 'last name',             'email' => 'email',             'mobile' => 'mobile',             'gender' => 'gender',             'profile_pic' => 'profile pic',             'address' => 'address',             'city' => 'city',             'state' => 'state',             'country' => 'country',             'pincode' => 'pincode',             'status' => 'status',              'created_date' => 'created date',             'updated_by' => 'updated by',             'updated_date' => 'updated date',             'username' => 'username',             'auth_key' => 'auth key',             'password_hash' => 'password',             'password_reset_token' => 'password reset token',             'file' => 'profile picture',         ];     }      /**      * generates password hash password , sets model      *      * @param string $password      */     public function setpassword($password)     {         $this->password_hash = yii::$app->security->generatepasswordhash($password);         return "$this->password_hash";     }      /**      * generates "remember me" authentication key      */     public function generateauthkey()     {         $this->auth_key = yii::$app->security->generaterandomstring();         return "$this->auth_key";     } } 

view's code:

use yii\helpers\html; use yii\widgets\activeform; use yii\helpers\arrayhelper; use backend\models\citymaster; use backend\models\statemaster; use backend\models\countrymaster; /* @var $this yii\web\view */ /* @var $model backend\models\clientusers */ /* @var $form yii\widgets\activeform */ ?>  <div class="client-users-form">     <div class="row">      <?php $form = activeform::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>      <div class="col-sm-6">     <?= $form->field($model, 'first_name')->textinput(['maxlength' => true]) ?>     </div>     <div class="col-sm-6">     <?= $form->field($model, 'last_name')->textinput(['maxlength' => true]) ?>     </div>     </div>      <div class="row">     <div class="col-sm-6">     <?= $form->field($model, 'email')->textinput(['maxlength' => true]) ?>     </div>     <div class="col-sm-6">     <?= $form->field($model, 'mobile')->textinput(['maxlength' => true]) ?>      </div>     </div>      <div class="row">     <div class="col-sm-6">      <?= $form->field($model, 'gender')->radiolist(array('1'=>'male','0'=>'female')); ?>     </div>      <div class="col-sm-6">      <?=    $form->field($model, 'file')->fileinput();      ?>      </div>     </div>     <div class="row">     <div class="col-sm-6">      <?= $form->field($model, 'address')->textinput(['maxlength' => true]) ?>     </div>      <div class="col-sm-6">      <?= $form->field($model, 'country')->dropdownlist(             arrayhelper::map(countrymaster::find()->all(),'id','country_name'),[             'prompt'=>'select country',             'onchange'=>'                     $.post("index.php?r=state/lists&id='.'"+$(this).val(), function(data){                         $("select#clientusers-state").html(data);                         });'             ]     )->label('country'); ?>     </div>     </div>      <div class="row">     <div class="col-sm-6">     <?= $form->field($model, 'state')->dropdownlist(             arrayhelper::map(statemaster::find()->all(),'id','state_name'),['prompt'=>'select state name']     )->label('state'); ?>     </div>     <div class="col-sm-6">      <?php     //$form->field($model, 'city')->textinput()      ?>      <?= $form->field($model, 'city')->dropdownlist(             arrayhelper::map(citymaster::find()->all(),'id','name'),[             'prompt'=>'select city',              ]     )->label('city'); ?>     </div>     </div>     <div class="row">     <div class="col-sm-6">     <?= $form->field($model, 'pincode')->textinput() ?>     </div>     <div class="col-sm-6">    <?= $form->field($model, 'status')->dropdownlist(array('10'=>'active','0'=>'inactive')); ?>    </div>    </div>    <div class="row">     <div class="col-sm-6">        <?= $form->field($model, 'username')->textinput(['maxlength' => true]) ?> </div>     <div class="col-sm-6">       <?= $form->field($model, 'password_hash')->passwordinput(['maxlength' => true]) ?> </div> </div>       <div class="form-group">         <?= html::submitbutton($model->isnewrecord ? 'create' : 'update', ['class' => $model->isnewrecord ? 'btn btn-success' : 'btn btn-primary']) ?>     </div>      <?php activeform::end(); ?>  </div> 

in code don't check model validate

 if($model->validate()) {      // code validate true  } 

however validate() doesn't show error. justs check validation rules , answers true or false.

for testing can try:

$model->save(false); 

in way, model saved without checking validation. if in way, model saved, mean have validation rules not correct or data not respecting them.


Comments

Popular posts from this blog

php - Admin SDK -- get information about the group -

dns - How To Use Custom Nameserver On Free Cloudflare? -

Python Error - TypeError: input expected at most 1 arguments, got 3 -