Wednesday, June 23, 2010

Unexpected switch from Edit to Add Form and Problems with validation

Hi,

I'm having a problem I really can't even begin to explain; I'd
appreciate any tips you could give.

I have a form to edit a user's information. For one the validation
does not seem to be working correctly. If I enter "Sarah" as a first
name (should validate and save) the form reappears with the password
fields blank (my else condition for the save, meaning the form either
didn't validate or couldn't save), and no validation error messages.
I try again but make an invalid first name: the validation errors will
pop up after pressing submit. Now if I try to once again enter
"Sarah" for the first name, the add (rather than edit) form reappears
with the data from the user that was trying to do the edit (only the
fields in common are displayed) and the following error messages:

Notice (8): Undefined index: password [APP/controllers/
users_controller.php, line 101]

Notice (8): Undefined offset: 0 [CORE/cake/libs/model/model.php, line
1009]


Line 101 is in add(), not edit(); notice the line is marked in the
code below. Add() uses 'password' as an array index (edit() never
uses password).
The only function in the model is called ONLY by edit().

So I suppose I have two questions:

1) Why is the data not validating/saving? I presume that the problem
is not saving, because there are no problems validating and saving
with the add form.

2) Why is the form switching from edit to add?

Code for add, edit, and the model function below. I would be happy to
post any other code that you need to help me figure out why this
problem is occurring. Note: this is cake 1.2.

function add() {

$this->layout = 'white';
if (!empty($this->data)) {
$this->data['User']['password'] =
Security::hash($this->data['User']['password'], 'sha1', true);//LINE
101
$this->data['User']['confirm_password'] =
Security::hash($this->data['User']['confirm_password'], 'sha1', true);
//set group to student (student = group_id =
6)
$this->data['User']['group_id'] = 6;
$this->data['User']['active'] = 1;
$this->User->create();
if ($this->User->save($this->data)) {
$this->flash(__('Congratulations, you
have created your eVoices account! Click here, and login.', true),
array('controller'=>'pages', 'action'=>'home'));
}
else {
$this->data['User']['password'] = '';
$this->data['User']
['confirm_password'] = '';
}
}

$animals = $this->User->Animal->find('list');
$this->set(compact('animals'));
}


function edit($id = null) {
$this->layout = 'white';
//invalid user
if (!$id /*&& empty($this->data)/*/) {
$this->flash(__('Invalid User', true),
array('action'=>'profile'));
}


//if form submitted
if (!empty($this->data)) {

$this->data['User']['current_password'] =
Security::hash($this->data['User']['current_password'], 'sha1', true);
//check password

if (!$this->User->verifyPassword($this-
>data['User']['current_password'], $this->obAuth->getUserId())) {
$this->flash(__('The entered password
is not the current password. Changes not saved.', true),
array('action'=>'edit', $id));
}
//password correct
else {
//if password changed
if ($this->data['User']
['new_password'] != Security::hash('', 'sha1', true) && ($this-
>data['User']['new_password']==$this->data['User']
['confirm_password'])) {
$this->data['User']
['password'] = Security::hash($this->data['User']['confirm_password'],
'sha1', true);
//include password in list of
fields to be saved
$fieldList =
array('firstname', 'lastname', 'email', 'animal_id', 'password');
}
//if password not changed, do not
include password in list of fields to be saved
else {
$fieldList =
array('firstname', 'lastname', 'email', 'animal_id');
}
if ($this->User->save($this->data,
true, $fieldList)) {
$this->flash(__('The User has
been saved.', true), array('action'=>'profile'));
} else {
$this->data['User']
['current_password'] = '';
$this->data['User']
['new_password'] = '';
$this->data['User']
['confirm_password'] = '';
//No flash message: want page
to reload with validations
//$this->flash(__('There was a
problem saving this information.', true), array('action'=>'profile'));
}
}
}
if (empty($this->data)) {
$this->data = $this->User-
>read(array('username', 'firstname', 'lastname', 'email',
'animal_id'), $id);
}
$animals = $this->User->Animal->find('list');
$groups = $this->User->Group->find('list');
$this->set(compact('animals','groups'));


}


/* verifyPassword
* Used by function edit in users_controller.php to check that
a password matches the stored password.
*/
function verifyPassword($pw, $id) {
if (!$pw || !$id){
return false;
}

$storedPW = $this->read(array('password'), $id);
$storedPW = $storedPW['User']['password'];

if ($storedPW == $pw) {
return true;
}
return false;

}

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 cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: