data validate in rule section in yii2

I created CRUD in Yii2. I used class extend Model and i think i do all right but when i tried to validate my data i get errors that attributes cannot be blanc. When i dont used validate my aplication save data to datatable corretly. This is my action create and update in controller:


public function actionCreate()

    {

        $model = new UrUserForm();


        $model->scenario = 'create';

        var_dump($model->validate(), $model->getErrors());

        exit();

        if ($model->load(Yii::$app->request->post())&& $model->saveUser()) {

           Yii::$app->session->setFlash('success', 'Użytkownik został dodany');

            return $this->redirect(['index']);

        } else {

            return $this->render('create', [

                'model' => $model,


            ]);

        }

    }


    public function actionUpdate($id)

    {

        $model = UrUserForm::findOne($id);

        if (!$model) {

            Yii::$app->session->setFlash('error', 'Niestety wystąpił błąd. Przosze skontaktować się z administratorem');

            return $this->redirect(['index']);

        }

        $model->scenario = 'update';

        if ($model->load(Yii::$app->request->post()) && $model->updateUser()) {

            Yii::$app->session->setFlash('success', 'Dane zostały zapisane');

            return $this->redirect(['index']);

        } else {

            return $this->render('update', [

                'model' => $model,


            ]);

        }

    }

I wrote var_dump() above in create action, to check whether the data are and when i filled all my inputs i have this message:


bool(false) array(7) { ["Login"]=> array(1) { [0]=> string(22) "Login cannot be blank." } ["Sex"]=> array(1) { [0]=> string(23) "PĹeÄ cannot be blank." } ["Name"]=> array(1) { [0]=> string(22) "ImiÄ cannot be blank." } ["Surname"]=> array(1) { [0]=> string(25) "Nazwisko cannot be blank." } ["BirthDate"]=> array(1) { [0]=> string(31) "Data urodzenia cannot be blank." } ["Email"]=> array(1) { [0]=> string(22) "Email cannot be blank." } ["Password"]=> array(1) { [0]=> string(23) "HasĹo cannot be blank." } }

When i delete var_dump it save corretly but i know i should validate this data. There is my UrUserForm:


<?php


namespace backend\modules\users\models;


use common\models\User;

use backend\modules\users\models\UrUser;

use yii\base\Model;

use Yii;

use yii\helpers\Url;




class UrUserForm extends Model {


    public $Login;

    public $Email;

    public $Password;

    public $Sex;

    public $Country;

    public $Language;

    public $Category;

    public $AccoutType;

    public $Name;

    public $Surname;

    public $BirthDate;

    public $RulesAccept;

    public $user;


    public function rules() {

        return [

            [['Country', 'Language', 'Category'], 'safe'],

            ['Login', 'filter', 'filter' => 'trim'],

            [['Login', 'Sex', 'Name', 'Surname', 'BirthDate'], 'required'],

            ['Login', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Pdany login jest już używany')],

            ['Login', 'string', 'min' => 2, 'max' => 255],

            ['Email', 'filter', 'filter' => 'trim'],

            ['Email', 'required'],

            ['Email', 'email'],

            ['Email', 'string', 'max' => 255],

            ['Email', 'unique', 'targetClass' => '\common\models\User', 'message' => Yii::t('app', 'Podany e-mail jest już używany')],

            ['Password', 'string', 'min' => 6],

            ['Password', 'required','on' => 'create'],

        ];

    }


    public function saveUser() {


        $user = new UrUser();

        $user->Login = $this->Login;

        $user->Email = $this->Email;

        $user->RulesAccept = 1;

        $user->Rel_Sex = $this->Sex;

        $user->Name = $this->Name;

        $user->BirthDate = $this->BirthDate;

        $user->Surname = $this->Surname;

        $user->setPassword($this->Password);

        $user->generateAuthKey();

        $user->Rel_Country = $this->Country;

        $user->Rel_UserCategory = $this->Category;

        $user->Rel_Language = $this->Language;

        $user->status = 10;

        $this->user = $user;

        $user->created_at=time();

        if ($this->validate() && $user->validate()) {

            $user->save();

            return $user;

        }

        return false;

    }


    public function updateUser() {

        $this->user->load($this->toArray(), '');

        $this->user->Rel_Country=$this->Country;

        $this->user->Rel_Language=$this->Language;

        $this->user->Rel_UserCategory=$this->Category;

        $this->user->Rel_Sex=$this->Sex;

        $this->user->updated_at=time();

        if (!empty($this->Password)) {

            $this->user->setPassword($this->Password);

        }


        return $this->user->save();

    }







    public static function findOne($id) {

        $user = UrUser::find()->where(['Id' => $id])->one();

        $model = new UrUserForm();

        $model->load($user->toArray(), '');

        $model->Country=$user->Rel_Country;

        $model->Language=$user->Rel_Language;

        $model->Category=$user->Rel_UserCategory;

        $model->scenario = 'update';

        $model->Sex=$user->Rel_Sex;

        $model->user = $user;

        return $model;

    }


}

I dont know why i have this error when i want to validate my data anyone can help me?

I would be interested to know why would you use var_dump() and exit(). Thanks. I was following the example logic which was shown in the signup form which we get along with advanced template.

i used var_dump to check that data is pass the validation. It is wrong way?




  var_dump($model->validate(), $model->getErrors());

        exit();

        if ($model->load(Yii::$app->request->post())&& $model->saveUser()) {



You validate model before you fill it with data, and so you get an error




if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->saveUser()) {

....

}



really… sorry i miss that