开发者

Symfony 1.4 - Don't save a blank password on a executeUpdate action

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 t开发者_JAVA技巧here 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.


It looks fine to me:

public function setPassword($password)
{
  if (!$password) return false;
  return $this->_set('password', UserProfile::encryptPassword($password));
}

I've got a few forms where I need to update sfGuardUser password along with other form fields that relate to different models, and I've ended up with something like this:

$id = // sfGuardUser primary key
$values['password'] = // the form post value returned

if ($values['password']) {
  $user = Doctrine::getTable('sfGuardUser')->find($id);
  $user->setPassword($values['password']);
  $user->save();
}

... saves a value if a value was posted in the form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜