Hello!
I do registration and authorization in yii2 and I just can not understand why validation fails in the form model. In the controller, the load () method returns false.

Controller:

use Yii; use app\models\auth\Login; class UserController extends \yii\web\Controller { //ЭкшСн ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ public $defaultAction = 'login'; public function beforeAction($action) { /////////////////////////////////////////////////////////////// //FOR DEBUG $this->enableCsrfValidation = false; return parent::beforeAction($action); ////////////////////////////////////////////////////////////// } public function actionLogin() { if (!Yii::$app->user->isGuest) { //TODO } $model = new Login(); //Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; if ($model->load(Yii::$app->request->post()) && $model->save()) { print('ok'); } return $this->render('login'); } public function actionRegister() { return $this->render('register'); } public function actionPassword() { } } 

Form Model:

 namespace app\models\auth; use Yii; use yii\base\Model; class Login extends Model { public $email; public $phone; public $password; public $rememberMe = true; public $typeLogin; private $_user = false; public function rules() { return [ [['password'], 'required'], ['email', 'validateLogin'], ['phone', 'validateLogin'], ['password', 'validatePassword'], ['rememberMe', 'boolean'], ]; } /* * Валидация Π²Π²Π΅Π΄Π΅Π½ΠΎΠ³ΠΎ адрСса эл. ΠΏΠΎΡ‡Ρ‚Ρ‹ ΠΈΠ»ΠΈ Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½Π° * @return bool */ public function validateLogin($model, $attribute) { //TODO if ($this->email) { $this->typeLogin = 'email'; return; } if ($this->phone) { $this->typeLogin = 'phone'; return; } $this->addError($attribute, 'ΠΠ΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎΠ΅ имя ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΈΠ»ΠΈ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ!'); } /* * Валидация пароля * @return bool */ public function validatePassword($model, $attribute) { if ( !$this->hasErrors() ) { $user = $this->getUser(); if (!$user || !$user->validatePassword($this->password)) { $this->addError($attribute, 'ΠΠ΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎΠ΅ имя ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΈΠ»ΠΈ ΠΏΠ°Ρ€ΠΎΠ»ΡŒ!'); } } } /* * Авторизация ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ * @return bool */ public function login() { if ($this->validate()) { return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } return false; } /* * Поиск ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΏΠΎ Π»ΠΎΠ³ΠΈΠ½Ρƒ ΠΈΠ»ΠΈ Π½ΠΎΠΌΠΊΡ€Ρƒ Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½Π° * @return bool */ public function getUser() { if ($this->_user === false) { if ($this->typeLogin === 'email') $this->_user = User::findByEmail($this->email); else $this->_user = User::findByPhone($this->phone); } return $this->_user; } //public function load($data) //{ // $this->email = isset($data['email']) ? $data['email'] : null; // $this->phone = isset($data['phone']) ? $data['phone'] : null; // $this->password = isset($data['password']) ? $data['password'] : null; // $this->rememberMe = isset($data['rememberMe']) ? $data['rememberMe'] : null; // parent::load($data); //} } 

Identification Model:

 <?php namespace app\models\auth; use Yii; use yii\db\ActiveRecord; use yii\web\IdentityInterface; use yii\behaviors\TimestampBehavior; /** * @property integer $id * @property string $email * @property string $phone * @property string $password * @property string $name * @property string $sename * @property string $fathername * @property integer $group_id * @property integer $enabled * @property string $last_ip * @property string $created * @property integer $subscribed_on_news * @property integer $downloaded_to_unisender */ class User extends ActiveRecord implements IdentityInterface { private $salt = '79d6e182b3c811c559e6b'; public static function tableName() { return 's_users'; } public function rules() { return [ [['email', 'phone', 'name', 'sename'], 'required'], [['group_id', 'enabled', 'subscribed_on_news', 'downloaded_to_unisender'], 'integer'], [['created'], 'safe'], [['email', 'password', 'name', 'fathername'], 'string', 'max' => 255], [['phone', 'sename'], 'string', 'max' => 30], [['last_ip'], 'string', 'max' => 15], [['phone'], 'unique'], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'email' => 'Email', 'phone' => 'Phone', 'password' => 'Password', 'name' => 'Name', 'sename' => 'Sename', 'fathername' => 'Fathername', 'group_id' => 'Group ID', 'enabled' => 'Enabled', 'last_ip' => 'Last Ip', 'created' => 'Created', 'subscribed_on_news' => 'Subscribed On News', 'downloaded_to_unisender' => 'Downloaded To Unisender', ]; } /* * ПовСдСния */ public function behaviors() { return [ TimestampBehavior::className() ]; } /* * Поиск ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΏΠΎ email * @param string $email * @return */ public function findByEmail($email) { return static::findOne(['email' => $email ]); } /* * Поиск ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ ΠΏΠΎ Π½ΠΎΠΌΠ΅Ρ€Ρƒ Ρ‚Π΅Π»Π΅Ρ„ΠΎΠ½Π° * @param string $email * @return */ public function findByPhone($phone) { return static::findOne(['phone' => $phone ]); } /* * ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° пароля * @parem string $password * @return bool */ public function validatePassword($password) { return $this->password === md5($this->salt.$password.md5($password)); } /* * Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅/ИзмСнСниС пароля */ public function setPassword($password) { $this->password = md5($this->salt.$password.md5($password)); } /* * ГСнСрация случайной строки для ΠΈΠ½Π΄Π΅Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ†ΠΈΠΈ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ, * Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ ΠΌΠΎΠΆΠ΅Ρ‚ понадобится Π² ΠΎΠΏΡ†ΠΈΠΈ "Π·Π°ΠΏΠΎΠΌΠ½ΠΈΡ‚ΡŒ мСня" */ public function generateAuthKey() { $this->authKey === $app->security->generateRandomString(); } /* РСализация ΠΌΠ΅Ρ‚ΠΎΠ΄ΠΎΠ² интСрфСйса IdentityInterface */ public static function findIdentity($id) { return static::findOne($id); } public static function findIdentityByAccessToken($token, $type = null) { //return static::findOne(['access_token' => $token]); } public function getId() { return $this->id; } public function getAuthKey() { return $this->authKey; } public function validateAuthKey($authKey) { return $this->authKey === $authKey; } } 

    2 answers 2

    If the login form is generated using ActiveForm, then the names of the fields will be in this format (approximately):

     LoginForm[username] // LoginForm - Π½Π°Π·Π²Π°Π½ΠΈΠ΅ Ρ„ΠΎΡ€ΠΌΡ‹ LoginForm[password] // Аналогично 

    If you have your own html code on the login form, then in this:

     username password 

    The load () method also takes the name of a form. You can specify an empty value, then everything will work. Example:

     if ($model->load(Yii::$app->request->post(), '') && $model->save()) { print('ok'); } 

      The request must contain your array with the key of your form.

        [LoginForm] => Array ( [email] => mail@gmail.com [password] => 123456 ) 

      If the LoginForm key is not present, then the load method will return false.