0

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.

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I was thinking there might be something that could be done with post validators.
Yes, there probably is, but it's just a question of how much time you want to spend on something like this. I would go with your current working solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.