Hi @ all,
I just migrate my project from Yii1.1. to Yii2 and have some problems with the validation rules.
I’m programming a login for my user and read the documentation, but ti doesn’t work!
My User-Model extends from ActiveRecord
class User extends ActiveRecord implements IdentityInterface
{
public $authKey;
public $accessToken;
public $iDropDown;
const SCENARIO_LOGIN = 'login';
const SCENARIO_RESET = 'reset';
const SCENARIO_REGISTER = 'register';
public function scenarios()
{
$aScenarios = parent::scenarios();
$aScenarios[self::SCENARIO_LOGIN] = ['username', 'password'];
$aScenarios[self::SCENARIO_RESET] = ['email'];
$aScenarios[self::SCENARIO_REGISTER]= ['username', 'email', 'password'];
return $aScenarios;
}
public static function tableName()
{
return 'users';
}
public function rules()
{
return [
[['email', 'password'], 'required'],
[
'email', 'required',
'message' => 'Please enter a valid email address',
],
[
'password', 'required',
'message' => 'Please enter a valid password',
]
];
}
My Controller
public function actionLogin()
{
$this->layout = 'blank';
if(!Yii::$app->user->isGuest)
{
$this->goHome();
}
$aError = null;
if(Yii::$app->request->post())
{
$sEmail = Yii::$app->request->post('email', null);
$sPassword = Yii::$app->request->post('password', null);
$oUser = User::find()
->select('id, password, email')
->where('email = :EMAIL')
->params([':EMAIL' => $sEmail])
->one();
if(($sEmail != null ) && ( $sPassword != null ))
{
if( $oUser != null )
{
if( $oUser->password == md5($sPassword) )
{
Yii::$app->user->login( $oUser );
$this->goHome();
}
else
{
$error = 'Password validation failed!';
}
}
}
}
$oLogin = new LoginForm();
return $this->render('login', [
'oLogin'=> $oLogin,
'error' => $aError,
]);
}
does anybody has an idea, why no validation rules are executed?
thx!