views:

396

answers:

2

I am trying to validate a user when they register to my application. Nothing is getting set to validationErrors, which is strange can anyone help me out?

Here is my MembersController

<?php
    class MembersController extends AppController {
var $name = 'Members';

var $components = array('RequestHandler','Uploader.Uploader');

function beforeFilter() {
 parent::beforeFilter();
 $this->layout = 'area';
 $this->Auth->allow('register');
 $this->Auth->loginRedirect = array('controller' => 'members', 'action' => 'dashboard');
 $this->Uploader->uploadDir = 'files/avatars/';
 $this->Uploader->maxFileSize = '2M';
}


function login() {}

function logout() {
  $this->redirect($this->Auth->logout());
}

function register() {
 if ($this->data) {
  if ($this->data['Member']['psword'] == $this->Auth->password($this->data['Member']['psword_confirm'])) {
   $this->Member->create();
   if ($this->Member->save($this->data)) {
    $this->Auth->login($this->data);
    $this->redirect(array('action' => 'dashboard'));
   } else {
    $this->Session->setFlash(__('Account could not be created', true));
    $this->redirect(array('action' => 'login'));
    pr($this->Member->invalidFields());
   }
  }
 }
}

} ?>

Member Model

<?php

class Member extends AppModel {

var $name = 'Member';
var $actsAs = array('Searchable'); 
var $validate = array(
 'first_name' => array(
        'rule' => 'alphaNumeric',
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter your first name'
    ),
 'last_name' => array(
        'rule' => 'alphaNumeric',
        'required' => true,
        'allowEmpty' => false,
        'message' => "Please enter your last name"
    ),
 'email_address' => array(
        'loginRule-1' => array(
            'rule' => 'email',  
            'message' => 'please enter a valid email address',
            'last' => true
         ),
        'loginRule-2' => array(
            'rule' => 'isUnique',  
            'message' => 'It looks like that email has been used before'
        )
 ),
 'psword' => array(
        'rule' => array('minLength',8),
        'required' => true,
        'allowEmpty' => false,
        'message' => 'Please enter a password with a minimum lenght of 8 characters.'
    )
);
var $hasOne = array('Avatar');

var $hasMany = array(
 'Favourite' => array(
  'className' => 'Favourite',
  'foreignKey' => 'member_id',
  'dependent' => false
 ),
 'Friend' => array(
  'className' => 'Friend',
  'foreignKey' => 'member_id',
  'dependent' => false
 ),
 'Guestbook' => array(
  'className' => 'Guestbook',
  'foreignKey' => 'member_id',
  'dependent' => false
 ),
 'Accommodation'
);

var $hasAndBelongsToMany = array('Interest' => array(
  'fields' => array('id','interest')
 )
);


function beforeSave($options = array()) {
 parent::beforeSave();
 if (isset($this->data[$this->alias]['interests']) && !empty($this->data[$this->alias]['interests'])) {
  $tagIds = $this->Interest->saveMemberInterests($this->data[$this->alias]['interests']);
  unset($this->data[$this->alias]['interests']);
  $this->data[$this->Interest->alias][$this->Interest->alias] = $tagIds;
 }
 $this->data['Member']['first_name'] = Inflector::humanize($this->data['Member']['first_name']);
 $this->data['Member']['last_name'] = Inflector::humanize($this->data['Member']['last_name']);
 return true;
}

} ?>

login.ctp

    <div id="login-form" class="round">
 <h2>Sign In</h2>
 <?php echo $form->create('Member', array('action' => 'login')); ?>
  <?php echo $form->input('email_address',array('class' => 'login-text',
             'label' => array('class' => 'login-label')
            ));?>
  <?php echo $form->input('psword' ,array('class' => 'login-text',
           'label' => array('class' => 'login-label','text' => 'Password')
          ))?>
 <?php echo $form->end('Sign In');?> 
</div> 
<div id="signup-form" class="round">
 <h2>Don't have an account yet?</h2>
 <?php echo $form->create('Member', array('action' => 'register')); ?>
  <?php echo $form->input('first_name',array('class' => 'login-text',
             'label' => array('class' => 'login-label')
            ));?>
  <?php echo $form->input('last_name',array('class' => 'login-text',
             'label' => array('class' => 'login-label')
            ));?>
  <?php echo $form->input('email_address',array('class' => 'login-text',
             'label' => array('class' => 'login-label')
            ));?>
  <?php echo $form->input('psword' ,array('class' => 'login-text',
           'label' => array('class' => 'login-label','text' => 'Password')
          ))?>
  <?php echo $form->input('psword_confirm' ,array('class' => 'login-text',
              'label' => array('class' => 'login-label','text' => 'Confirm'),
              'div' => array('style' => ''),
              'type' => 'password'  
          ))?>
 <?php echo $form->end('Sign In');?>
</div>
+1  A: 

I believe your problem is here:

$this->redirect(array('action' => 'login'));
pr($this->Member->invalidFields());

The validation errors are designed to show on the form, underneath the appropriate field. However, instead of continuing and trying to display the form, you are redirecting the user to a different page.

If you remove the two lines above, it should show the validation errors beneath their fields on the form when the validation fails.

Jason
A: 

Thank you!!!

$this->redirect was messing up my validation messages as well.

al
You should always use exit() after redirect.
powtac