Symfony 1.4 - Don't save a blank password on a executeUpdate action.
        Posted  
        
            by Twelve47
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Twelve47
        
        
        
        Published on 2010-04-22T11:18:44Z
        Indexed on 
            2010/04/22
            11:23 UTC
        
        
        Read the original article
        Hit count: 268
        
I have a form to edit a UserProfile which is stored in mysql db. Which includes the following custom configuration:
public function configure()
  {
    $this->widgetSchema['password']=new sfWidgetFormInputPassword();
    $this->validatorSchema['password']->setOption('required', false); // you don't need to specify a new password if you are editing a user.
  }
When the user tries to save the executeUpdate method is called to commit the changes. If the password is left blank, the password field is set to '', but I want it to retain the old password instead of overwriting it.
What is the best (/most in the symfony ethos) way of doing this? My solution was to override the setter method on the model (which i had done anyway for password encryption), and ignore blank values.
public function setPassword( $password )
{
  if ($password=='') return false; // if password is blank don't save it.
  return $this->_set('password', UserProfile ::encryptPassword( $password ));
}
It seems to work fine like this, but is there a better way?
If you're wondering I cannot use sfDoctrineGuard for this project as I am dealing with a legacy database, and cannot change the schema.
© Stack Overflow or respective owner