I am new to symfony and i am working on an existing project in symfony 1.4 version using sfDoctrineGuard Plugin . I am trying to update a user account in the back-end and it requires a new password to be set at the same time. The password field should not be a required field when updating a user. This are the steps that i am taking
1) Edit a user in the backend via "user accounts" 2) Save the user without making any changes, ensuring the password fields are both blank. 3) Form throws the following error: "password required"
Here is my code for the Edit Form
class sfGuardUserEditForm extends BasesfGuardUserForm
{
$this->widgetSchema['password'] = new sfWidgetFormInputPassword();
$this->validatorSchema['password'] = new sfValidatorPassword();
$this->widgetSchema['password_again'] = new sfWidgetFormInputPassword();
$this->validatorSchema['password_again'] = new sfValidatorString(array('max_length' => 128, 'required' => true, 'empty_value' => null), array('required' =>'Please provide password again' ));
$this->widgetSchema->moveField('password_again', 'after', 'password');
$this->mergePostValidator(new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 'password_again', array(), array('invalid' => 'The two passwords must be the same.')));
}
public function save($conn = null)
{
$this->object->setPassword($this->getValue('password'));
$this->object->save();
}
}
How can fix this?
You can also check if the object already exists or not 1st. If it exists it doesn't validate the password field on edit and if it doesn't exist it must validate the password field.
if ($this->getObject()->Exists())
{
$this->validatorSchema['password'] = new sfValidatorPassword(array('required'=>false));
}
else
{
$this->validatorSchema['password'] = new sfValidatorPassword(array('required'=>true));
}