BrianDHall - 07 December 2009 10:54 PM
I want to use a password_confirm field to control account creation, but obviously beyond account creation it is never used.
In this specific example, you don’t need to make it complicated. If you use matches and required on a password_confirm field, DMZ will automatically fill in the virtual password_confirm field when loading an existing object. The only time you need to do something special is when programmatically changing the password (like a generated password); then you’ll need to set both.
Besides, can’t the user change the password at a later date? They’ll need a password_confirm then, too.
—————
But, if you still want to create an only-required-when-new field, you’ll probably need to do this:
1) Create a custom required method on your object, that has a specific check for the confirmation field:
function required($field) {
if($field == 'password_confirm' && $this->exists()) {
// You could try setting password_confirm here
// This may not work if you are properly encrypting the password!
// $this->password_confirm = $this->password;
return TRUE;
} else {
return parent::required($field);
}
}
2) For any other validation rules used on that field, you’ll probably need to override them like above. (You might even want to add a method for doing the field/exists check, so you aren’t copying and pasting code all over.)
—————
I might add (yet another) validation rule required_when_new that handles this internally. It’s an easy addition.