更新用户帐户需要新密码


Updating a user account requires new password

我是symfony的新手,我正在使用sfDoctrineGuard插件在symfony 1.4版本的现有项目上工作。我试图在后端更新一个用户帐户,它需要在同一时间设置一个新的密码。在更新用户时,密码字段不应该是必填字段。这是我正在采取的步骤

1)在后端通过"user accounts"编辑一个用户2)保存用户,不做任何更改,确保密码字段均为空白。3)表单抛出以下错误:"password required"

这是我的代码编辑表单

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();
}
}

如何解决这个问题?

您需要更改后端用于用户编辑的表单,使其不需要密码。改变:

$this->validatorSchema['password'] = new sfValidatorPassword(array('required'=>false));

$this->validatorSchema['password_again'] = new sfValidatorString(array('max_length' => 128, 'required' => false, 'empty_value' => null), array('required' =>'Please provide password again' ));

删除了添加新密码的要求。

您也可以先检查对象是否已经存在。如果它存在,则在编辑时不验证密码字段,如果它不存在,则必须验证密码字段。

if ($this->getObject()->Exists())
{
$this->validatorSchema['password'] = new sfValidatorPassword(array('required'=>false));
}
else 
{
$this->validatorSchema['password'] = new sfValidatorPassword(array('required'=>true));  
}