Hi,

I'm using Cake 1.3.6, and I have a login page and a register page
working with my application using the Auth component, but as I try to
create a 'change password' page I'm finding that my validation
messages aren't showing up on my change_password.ctp page. I am using
different field names for this page than I do on the login and
register pages, but at the moment I've copied the validation rules.

(I've also searched this group, and couldn't find this problem here
already.)

Here's what a slightly trimmed-down version of my change_password.ctp
view page looks like:

  <?php
  echo $session->flash();
  echo $session->flash('auth');
  ?>

  <?php echo $this->Form->create('User', array('name' =>
'ChangePasswordForm'));?>
        <fieldset>
                <legend><?php __('Account Information :: Change Password'); ?></
legend>

        <?php
          // don't allow username or password to be changed at this time
      echo $this->Form->input('current_password', array('onfocus' =>
'this.select()'));
                echo $this->Form->input('new_password1', array('onfocus' =>
'this.select()'));
                echo $this->Form->input('new_password2', array('onfocus' =>
'this.select()'));
        ?>

    <div class="submit">
      <input type="submit" value="Save" />&nbsp;
      <span class="form-text">or</span>
      <a href="#" onclick="history.back(); return false;">back</a>
    </div>

        </fieldset>
  <?php echo $this->Form->end(); ?>

I'm doing the wonky submit stuff there at the end because of something
else I was trying, and I haven't switched it back to the normal Cake
way of doing things. Other forms work with that, so I don't think it's
a problem.

My User model is pretty long right now, and I'm not sure you'll want
it all here, but these are the validation rules I'm using for these
fields:

  'current_password' => array(
        'ruleRequired' => array(
            'rule' => 'notEmpty',
            'message' => 'Please enter your current Password',
            'required' => false,  # not always required in $this->data
        )
  ),
  'new_password1' => array(
        'charsAllowed' => array(
            'rule' => '/^[a-z0-...@#$%^&*()_+-=\[\]\\\|\{\},\.\?\/]
{6,}$/i',
            'message' => 'Sorry, there\'s a character in the Password
I don\'t like'
        ),
        'length' => array(
            'rule' => array('between', 6, 20),
            'message' => 'The Password must be between 6 and 20
characters'
        ),
        'ruleRequired' => array(
            'rule' => 'notEmpty',
            'message' => 'The Password field is required',
            'required' => false,  # not always required in $this->data
in all forms
        ),
  ),
  'new_password2' => array(
        'length' => array(
            'rule' => array('between', 6, 20),
            'message' => 'The Password must be between 6 and 20
characters'
        ),
        'ruleRequired' => array(
            'rule' => 'notEmpty',
            'message' => 'The Password field is required',
            'required' => false,  # not always required in $this->data
in all forms
        ),
  ),

I actually have a function I use to test that the two new password
fields match, but I don't get validation messages with or without that
function, so I've removed it from the validation code shown above.

Here's what my change_password function in the UsersController class
looks like at the moment:

  function change_password() {
    # get the id from the session, don't trust a passed-in id
    $id = $this->get_user_id();

    if (!empty($this->data)) {
      $this->data['User']['id'] = $id;

      # make sure 'current_password' equals their current password in
the database
      $user_curr_pwd_hash = $this->Auth->password($this->data['User']
['current_password']);
      $curr_db_passwd = $this->User->get_curr_db_password($id);

      # if these two passwords match, set 'password' in the form, and
try the save()
      if ($user_curr_pwd_hash == $curr_db_passwd)
      {
        # hash one of the two matching passwords; set it to 'password'
so cake will try to save it
        $this->data['User']['password'] = $this->Auth->password($this-
>data['User']['new_password1']);

        # try the normal save() process
        if ($this->User->save($this->data)) {
          $this->Session->setFlash(__('Your account information has
been saved', true));
          $this->redirect(array('controller' => 'users', 'action' =>
'view'));
        } else {
          $this->Session->setFlash(__('Sorry, a problem occurred
trying to save your account information. Please, try again.', true));
        }
      }
    }

    if (empty($this->data)) {
      $this->data = $this->User->read(null, $id);
    }
    $this->set('user', $this->User->read(null, $id));
  }

  function get_user_id() {
    $u = $this->Auth->user();
    return $u['User']['id'];
  }


I also have a before_filter method in the controller that looks like
this:

  function beforeFilter()
  {
    // tell Auth to not ask for authentication when doing the
'register' action
    $this->Auth->allow('register');
  }

I was thinking the problem might be there, but I've tried different
ways of including a reference to the change_password function, but
that hasn't helped.

My 'users' database table doesn't include any of the fields on this
form. The fields I do have in my users database table are these:

  create table users (
    id int unsigned auto_increment not null primary key,
    company_id int unsigned not null,
    username varchar(50) not null unique,
    password varchar(40) not null,
    first_name varchar(32),
    last_name varchar(32),
    email_address varchar(128) not null unique,
    last_login timestamp not null default now(),
    foreign key (company_id) references companies(id) on delete
cascade
  ) ENGINE = InnoDB;


With this current setup, the following things happen when I try to use
the change_password.ctp form:

1) If I enter all the data right, I can change the old password
(current_password) to a new one.
2) If the data I enter fails a validation rule, the form is re-
displayed, and I see the "Sorry..." error message from the view's
"$session->flash();" code, but nothing else.

Any pointers on what I'm doing wrong, i.e., why I'm not seeing any
field-level validation messages on this form?

Thanks,
Al

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to