saji
saji

Reputation: 203

Hashing password before saving to databse in yii

I using yii framework for building a small application of a job site.And have encounterd errors mentioned below

My controller class is as follow:

  public function actionRegister()
{
        $model = new RegisterForm();
    if (isset($_POST['RegisterForm'])) {

        //  print_r($_POST); exit();

        $model->attributes = $_POST['RegisterForm'];
        if ($model->validate()) {
            $model->password = sha1($model['password']);
            $model->role ='user';
            $model->status='1';
            $model->created =date("Y-m-d H:i:s");
            $model->modified =date("Y-m-d H:i:s");

            if ($model->save()) {

                Yii::app()->user->setFlash('success', "Data saved!");
            } else {

                Yii::app()->user->setFlash('error', "Error,Cannot Save Data!");
            }
        } else {
            Yii::app()->user->setFlash('error', "Validation failed!");
        }
    }
    $this->render('register', array('model' => $model));
}

And Rules For validation as:

public function rules()
    {
               return array(
            // All Fields Required  //
            array('username, password,contact,email,name','required'),
                        array('email','email'),
                        array('contact', 'match', 'pattern'=>'/^[0-9]{1,15}$/'),
                        array('email','validateEmail'),
                        array('repassword', 'compare', 'compareAttribute'=>'password' ,'message'=>"Passwords don't match")

                    // password needs to be authenticated
            /*array('password', 'authenticate'),*/
        );
    }

I am encountering an error while submiting my data,perhaps it is due to confirm password prompting error password did not match even I type the same value in both fields.

Upvotes: 1

Views: 1522

Answers (1)

Andrii Mishchenko
Andrii Mishchenko

Reputation: 2694

Use

$model->save(false); // save with no validation

because you have already validated the model directly before

Upvotes: 2

Related Questions