更新用户帐户需要新密码

时间:2014-07-07 09:32:53

标签: php symfony-1.4 sfguard

我是symfony的新手,我正在使用sfDoctrineGuard插件处理symfony 1.4版本中的现有项目。我正在尝试更新后端的用户帐户,并且需要同时设置新密码。更新用户时,密码字段不应是必填字段。这是我正在采取的步骤

1)通过“用户帐户”在后端编辑用户 2)保存用户而不进行任何更改,确保密码字段均为空白。 3)表单抛出以下错误:“需要密码”

以下是我编辑表格的代码

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

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

您需要在后端更改用于编辑的表单,以便不需要密码。改变:

$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' ));

这消除了添加新密码的要求。

答案 1 :(得分:0)

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

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