CakePHP 3 step registration
Asked Answered
V

3

5

I am trying to create a 3 step registration page withh cakePHP. First step is OK, it inserts the data in db but the second step is not working. Can someone help please?

Here is my controller code:

<?php

App::uses('Controller', 'Controller');

class UsersController extends AppController {

public $name = 'Users';

public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('signup');
}

public function login(){
    if($this->request->is('post')){
        if($this->Auth->login()){
            $this->redirect($this->Auth->redirect());   
        } else {
            $this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg'));
        }
    }
}
public function logout(){
    $this->redirect($this->Auth->logout());
}
// SIGN UP ACTIONS
public function signup($step = null){
    // SET STEP VARIABLE
    $this->set('s', $step);
    // STEP 1 - REGISTER
    if(empty($step)){
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->write('regUser', $this->request->data['User']['email']);
                // $this->Session->setFlash('The user has been saved, please enter other information!',
                //                          'default', array('class' => 'okormsg'));
                $regUser = $this->User->find(   'first', array('conditions' => array(
                                                'User.username' => $this->Session->read('regUser'))));
                $id = $regUser['User']['id'];

                $this->Session->write('regUserId', $id);

                $this->redirect(array('personal-info'));
            } else {
                $this->Session->setFlash(   'There was an error, please check the fields bellow!',
                                            'default', array('class' => 'errormsg'));
            }
        }
    // STEP 2 - PERSONAL INFORMATION
    } elseif($step == 'personal-info') {
        if ($this->request->is('post')) {
            $id = $this->Session->read('regUserId');
            $this->User->id = $id;

            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash('The user has been saved');
                $this->redirect(array('complete'));
            } else {
                $this->Session->setFlash('User could not be saved. Please, try again.');
            }
        }
    // STEP 3 - COMPLETE REGISTRATION
    } elseif($step == 'complete') {

    }
}
}

?>

Here is the Model:

<?php

class User extends AppModel {

public $validate = array (
    'name'=>array(
        'Please enter your name!'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your name!'
        )
    ),
    'surname'=>array(
        'Please enter your surname!'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your surname!'
        )
    ),
    'email'=>array(
        'Valid email'=>array(
            'rule'=>array('email'),
            'message'=>'Please enter a valid Email address!'
        ),
        'Already exists'=>array(
            'rule'=>'isUnique',
            'message'=>'This Email is already registered in our database!'
        )
    ),
    'password'=>array(
        'Not empty'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your password!'
        ),
        'Match password'=>array(
            'rule'=>'matchPasswords',
            'message'=>'Your passwords do not match!'
        )
    ),
    'password_confirm'=>array(
        'Not empty'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please confirm your password!'
        )
    ),
    'terms'=>array(
        'Agree'=>array(
            'rule'=>'notEmpty',
            'required'=>true,
            'message'=>'You must agree to Terms and conditions!'
        )
    )
);

public function matchPasswords($data){
    if ($data['password'] == $this->data['User']['password_confirm']){
        return true;
    } else {
        $this->invalidate('password_confirm', 'Your passwords do not match!');
        return false;
    }
}
public function beforeSave(){
    if(!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    if (empty($this->data['User']['username'])) {
        $this->data['User']['username'] = $this->data['User']['email'];
    }
    return true;
}
}

?>

And here is the view for signup.ctp

<!--  SIGN UP STEPS  -->
<!--  SIGN UP - 1 (REGISTER)  -->
<?php if(empty($s)): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate active">Register</div>
        <div class="paginate">Personal Information</div>
        <div class="paginate">Complete Registration</div>
        <h1>User Registration</h1>
        <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
        <span style="color:#900">All field are required!</span></p>
    <?php
        echo $this->Form->create();
        echo $this->Form->input('name');
        echo $this->Form->input('surname');
        echo $this->Form->input('email');
        echo $this->Form->input('password');
        echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password'));
        echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox'));
        ?> I accept <a href="#" class="link">Terms Of Use</a> <?
        echo $this->Form->end('Continue Registration');
    ?>
</div>
</div>
<!--  SIGN UP - 2 (PERSONAL INFO)  -->
<?php elseif($s == 'personal-info'): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate">Register</div>
        <div class="paginate active">Personal Information</div>
        <div class="paginate">Complete Registration</div>
        <h1>User Registration</h1>
        <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
        <span style="color:#900">All field are required!</span></p>
    <?php
        echo $this->Form->create();
        echo $this->Form->input('phone');
        echo $this->Form->input('address');
        echo $this->Form->input('city');
        echo $this->Form->input('ptt', array('label'=>'Postal Code'));
        echo $this->Form->input('state');
        echo $this->Form->input('country');
        echo $this->Form->end('Complete Registration');
    ?>
</div>
</div>
<!--  SIGN UP - 3 (COMPLETE)  -->
<?php elseif($s == 'complete'): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate">Register</div>
        <div class="paginate">Personal Information</div>
        <div class="paginate active">Complete Registration</div>
        <h1>User Registration</h1>
        <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
        <span style="color:#900">All field are required!</span></p>
</div>
</div>
<? else: ?>
<div  id="register" class="container padded">
<div id="register">
    Unknown page!
</div>
</div>
<? endif; ?>

So as I said on first step it's all OK, it saves the user in DB but on second step I would like to put more information and when I submit it displays the Session flash message that User can not be saved, so that means that second user information is not saved in DB.

Please help!

Victoria answered 9/3, 2013 at 6:6 Comment(4)
can you tell what is not working? is the data is not saving in DB?Fairfield
Yes in first step it's saving but in second it's not saving in DB.Victoria
Could you clarify the issue that you're having? You say that the second step is not working, in what sense is it not working?Takeshi
OK, after entering data in first part its fine, saves in DB and redirects to other page and displays the view for that and when I enter more information and click submit it gives me an error 'User could not be saved. Please, try again.' And no changes in DB.Victoria
V
6

Found solution. There was an error in validating. On second step it tried to validate Terms of Use checkbox. So here is the complete code of 3 STEP REGISTRATION:

USER CONTROLLER:

<?php

App::uses('Controller', 'Controller');

class UsersController extends AppController {

public $name = 'Users';

public function beforeFilter(){
    parent::beforeFilter();
    $this->Auth->allow('signup');
}

public function login(){
    if($this->request->is('post')){
        if($this->Auth->login()){
            $this->redirect($this->Auth->redirect());   
        } else {
            $this->Session->setFlash('Invalid username or password!', 'default', array('class' => 'errormsg'));
        }
    }
}
public function logout(){
    $this->redirect($this->Auth->logout());
}
// SIGN UP ACTIONS
public function signup($step = null){
    // SET STEP VARIABLE
    $this->set('s', $step);
    // STEP 1 - REGISTER
    if(!$step){
        $this->set('r', 1);
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->write('regUser', $this->request->data['User']['email']);
                // $this->Session->setFlash('The user has been saved, please enter other information!',
                //                          'default', array('class' => 'okormsg'));
                $regUser = $this->User->find(   'first', array('conditions' => array(
                                                'User.username' => $this->Session->read('regUser'))));
                $id = $regUser['User']['id'];
                $this->Session->write('regUserId', $id);
                $this->redirect(array('personal-info'));
            } else {
                $this->Session->setFlash(   'There was an error, please check the fields bellow!',
                                            'default', array('class' => 'errormsg'));
            }
        }
    // STEP 2 - PERSONAL INFORMATION
    } elseif($step == 'personal-info') {
        $id = $this->Session->read('regUserId');
        $this->User->id = $id;
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data, array('validate' => false))) {
                $this->redirect(array('complete'));
            } else {
                $this->Session->setFlash(   'User could not be saved. Please, try again.',
                                            'default', array('class' => 'errormsg'));
            }
        }
    // STEP 3 - COMPLETE REGISTRATION
    } elseif($step == 'complete') {
        // Send email function
    }
}
}

USER MODEL:

<?php

class User extends AppModel {

public $validate = array (
    'name'=>array(
        'Please enter your name!'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your name!'
        )
    ),
    'surname'=>array(
        'Please enter your surname!'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your surname!'
        )
    ),
    'email'=>array(
        'Valid email'=>array(
            'rule'=>array('email'),
            'message'=>'Please enter a valid Email address!'
        ),
        'Already exists'=>array(
            'rule'=>'isUnique',
            'message'=>'This Email is already registered in our database!'
        )
    ),
    'password'=>array(
        'Not empty'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please enter your password!'
        ),
        'Match password'=>array(
            'rule'=>'matchPasswords',
            'message'=>'Your passwords do not match!'
        )
    ),
    'password_confirm'=>array(
        'Not empty'=>array(
            'rule'=>'notEmpty',
            'message'=>'Please confirm your password!'
        )
    ),
    'terms'=>array(
        'Agree'=>array(
            'rule'=>'notEmpty',
            'required'=>true,
            'message'=>'You must agree to Terms and conditions!'
        )
    )
);
public function matchPasswords($data){
    if ($data['password'] == $this->data['User']['password_confirm']){
        return true;
    } else {
        $this->invalidate('password_confirm', 'Your passwords do not match!');
        return false;
    }
}
public function beforeSave(){
    if(!empty($this->data['User']['password'])) {
        $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
    }
    if(!empty($this->data['User']['email'])){
        if (empty($this->data['User']['username'])) {
            $this->data['User']['username'] = $this->data['User']['email'];
        }
    }
}
}


?>

VIEW '/Users/signup.ctp'

<!--  SIGN UP STEPS  -->
<!--  SIGN UP - 1 (REGISTER)  -->
<?php if(empty($s)): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate active">Register</div>
        <div class="paginate">Personal Information</div>
        <div class="paginate">Complete Registration</div>
        <h1>User Registration</h1>
        <p>Welcome to user registration! You can sign up yourself and start improving your business. User registration is FREE and once you register yourself you can select apropiate business package for your company and start advertising.<br />
        <span style="color:#900">All field are required!</span></p>
    <?php
        echo $this->Form->create();
        echo $this->Form->input('name');
        echo $this->Form->input('surname');
        echo $this->Form->input('email');
        echo $this->Form->input('password');
        echo $this->Form->input('password_confirm', array('label'=>'Password Confirmation','type'=>'password'));
        echo $this->Form->input('terms', array('label'=>false, 'type'=>'checkbox'));
        ?> I accept <a href="#" class="link">Terms Of Use</a> <?
        echo $this->Form->end('Continue Registration');
    ?>
</div>
</div>
<!--  SIGN UP - 2 (PERSONAL INFO)  -->
<?php elseif($s == 'personal-info'): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate">Register</div>
        <div class="paginate active">Personal Information</div>
        <div class="paginate">Complete Registration</div>
        <h1>User Registration</h1>
        <p>This personal information are not required.<br />
    <?php
        echo $this->Form->create();
        echo $this->Form->input('phone');
        echo $this->Form->input('address');
        echo $this->Form->input('city');
        echo $this->Form->input('ptt', array('label'=>'Postal Code'));
        echo $this->Form->input('state');
        echo $this->Form->input('country');
        echo $this->Form->end('Complete Registration');
    ?>
</div>
</div>
<!--  SIGN UP - 3 (COMPLETE)  -->
<?php elseif($s == 'complete'): ?>
<div  id="register" class="container padded">
<div id="register">
        <div class="paginate">Register</div>
        <div class="paginate">Personal Information</div>
        <div class="paginate active">Complete Registration</div>
        <h1>Congratulation!</h1>
        <p>Your account has been created and <strong>Email</strong> has been send to your inbox. Please check you inbox and verify address by clicking on the link provided in mail.</p>
</div>
</div>
<? else: ?>
<div  id="register" class="container padded">
<div id="register">
    Unknown page!
</div>
</div>
<? endif; ?>

If someone wants to make registration with more steps, the rest is just the same!

Victoria answered 9/3, 2013 at 6:6 Comment(0)
A
4

You should consider splitting the steps into individual actions and views to make things simpler and easier to read/debug.

Step 1

Since you will create the User account at this point, you should automatically log in the user once saved and validated. Then you don't have to persist the information in the Session, which is bad in my experience.

An email can be sent to the user at this point from Model afterSave()

Step 2

I would move this to an action called "profile" which allows a user to update their additional profile information. Since they are already logged in you can easily find() the User and save their profile. This can be reused in the future too.

Step 3

This is just the "thank you" page from what I can see. You can redirect to the PagesController and render a simple thankyou.ctp rather than using the User Controller action which does nothing for the view.

The login can still happen even though their email address is not verified. You would only allow access to certain parts of the website until they have clicked the link sent to them in an email.

Acropolis answered 9/3, 2013 at 11:58 Comment(0)
G
1

try checking $step as below.

if(!$step){

empty is used to check if array is empty or not.

Gelignite answered 9/3, 2013 at 6:9 Comment(2)
That works fine, I have all 3 pages displayed correctly but when I try to submit second it's giving me an error.Victoria
User could not be saved. Please, try again. That's Session flash if not success.Victoria

© 2022 - 2024 — McMap. All rights reserved.