Search code examples
phpyiiyii-validation

$model->validate() always return false


I read similar topics of this problem, but I don't found solution.I have a form which load via ajax. When all data is valid, they don't save in database and show me validate error in console.in my User model I **set return parent::beforeSave(); in beforeSave method ** Here is my action:

          public function actionRegister()
{       
                $model = new RegistrationForm;
                if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
                {
                    $this->ajaxValidate($model);
                }
                if(isset($_POST['RegistrationForm']))
                {
                    if($model->validate())
                    {
                     $model->attributes =  $_POST['RegistrationForm'];
                     $user = new User;
                     $user->attributes = $model->attributes;
                     if($user->save())
                        echo 1;
                    }else{
                          $errors = $model->getErrors();
                            var_dump($errors);
                            exit;
                    }
                }
                 $this->renderPartial('register', array('model' => $model),false,true);
     public function ajaxValidate($model)
    {   
                echo CActiveForm::validate($model); 
                Yii::app()->end();     
    }

Solution

  • I just move

    $model->attributes =  $_POST['RegistrationForm'] 
    

    and set before

    if($model->validate())