0

Im my project I have issues with using the confirm password.

I don't have confirm password field in my database, I assume that is the reason I am getting this error.

My Model

     public $user_password_hash_repeat;
     public $agree;
     public $user_search;



//  const SCENARIO_LOGIN = 'login';
    const SCENARIO_REGISTER = 'signup';
    const SCENARIO_CREATE = 'create';
    const SCENARIO_UPDATE = 'update';
    const SCENARIO_PASSWORD = 'password';
    const SCENARIO_NEWPASSWORD = 'newpassword';


    public static function tableName()
    {
        return 'sim_user';
    }

    /** 
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['user_email', 'user_fname'], 'required', 'on' => 'update'], // on Update Method scenario
            [['user_email', 'user_fname', 'user_password_hash'], 'required', 'on' => 'create'], // on create method scenario
            [['user_fname','user_email','user_password_hash','user_password_hash_repeat'], 'required', 'on' => 'signup'], // on signup scenario
            [['user_password_hash'],'required' ,'on' => 'newpassword'],// repeat password scenario    
            ['agree','required','requiredValue' => 1,'message' => 'Tick the box', 'on' => 'signup'],// on signup scenario
            ['user_password_hash','match','pattern'=>'$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$','message'=>'Password must have atleast 1 uppercase and 1 number '],
            ['user_password_hash', 'string', 'min' => 6],
            ['user_password_hash_repeat', 'compare', 'compareAttribute'=>'user_password_hash', 'skipOnEmpty' => false],
            [['user_email'],'email'],
            [['user_company', 'user_suspended', 'user_deleted'], 'integer'],
            [['user_email'], 'string', 'max' =>255],
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'create'], //create method scenario for unique email
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'signup'],// signup method scenario for unique email
            ['user_email','unique','targetClass'=> '\app\models\SimUser','on'=> 'update'],//update method scenario for unique email
            [['user_fname', 'user_lname'], 'string', 'max' => 45],
            [['user_auth_key'], 'string', 'max' => 32],
            [['user_access_token'], 'string', 'max' => 100],
            ['user_email', 'exist', 'on' => self::SCENARIO_PASSWORD]


        ];
    }

Controller action

public function actionSignup()
    {
        $company = new Company(); 
        $model = new SimUser(['scenario' => SimUser::SCENARIO_REGISTER]);
        if ($model->load(Yii::$app->request->post())&& $model->validate() && $company->load(Yii::$app->request->post())&& $company->validate()) {
           // var_dump($model); exit()
            $model->setPassword($model->user_password_hash);
            $model->generateAuthKey();      
           // $company->save();

            $model->company_id = 1; 
              try{
                    $model->save();
                }catch (Exception $ex) {
                    var_dump($ex->getMessage()); exit();
                }

            if ($model->save()){
                $auth = Yii::$app->authManager;
                $authorRole = $auth->getRole('staff');
                $auth->assign($authorRole, $model->user_id);
            }
          \Yii::$app->user->login($model);
          return $this->redirect(['/site/index']);  
        }

        return $this->render('signup', [
            'model' => $model,
            'company' => $company,

        ]);
    }

If you see the rules, I have set user_password_hash_repeat to be skipOnEmpty = false, which forces the user to type in this field. If I set that to true and just type the password and submit it submits the form and saves the model successfully.

I have also tried using the required for the user_password_hash_repeat, I end up not able to save the model.

What is wrong in my rules setup?

Thank you!!

17
  • What error are you getting? Commented Nov 3, 2016 at 15:56
  • I don't get any error , It just doesn't save my model... Commented Nov 3, 2016 at 15:59
  • Try to use $model->scenario = 'signup'; before saving your model. Commented Nov 3, 2016 at 16:03
  • Show your related controller/action Commented Nov 3, 2016 at 16:04
  • I have added the controller action.. Commented Nov 3, 2016 at 16:07

1 Answer 1

0

It seems you are missing second parameter of load model, pass empty string as second parameter.

Replace this

$model->load(Yii::$app->request->post())

With this

$model->load(Yii::$app->request->post(), '') //Note: Pass second parameter as empty string

so your if condition would look like

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

hope this works.

Sign up to request clarification or add additional context in comments.

1 Comment

I added them..when I add this it submits the form but doesn't save anything and renders back the same page.. it doesn't even redirect my page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.