2010-02-07 73 views
0

我需要做的是通過添加'passwordagain'字段並將該值設置爲等於'password'字段的新字段來自定義我的默認userForm。 我的代碼:如何將默認值設置爲symfony表單中的額外字段

class UserForm extends BaseUserForm 
{ 
public function configure() 
{ 

$this->widgetSchema['password'] = new sfWidgetFormInputPassword(); 
$this->widgetSchema['passwordagain'] = new sfWidgetFormInputPassword(); 

$this->validatorSchema['password'] = new sfValidatorString(); 
$this->validatorSchema['passwordagain'] = new sfValidatorString(); 

$this->getValidatorSchema()->setPostValidator(
    new sfValidatorSchemaCompare(
    'password', 
    sfValidatorSchemaCompare::EQUAL, 
    'passwordagain', 
    array(), 
    array('invalid' => 'Passwords don\'t match') 
)); 

$this->setDefault('passwordagain', $this->getObject()->getPassword()); 

$this->useFields(array('username', 'password', 'passwordagain')); 

}}

的setDefault方法似乎有沒有工作。也許它只適用於新用戶,但這不是我在這裏尋找的。

謝謝, 拉杜。

P.S:順便說一句...我使用symfony的1.4推進

回答

2

默認是不工作的原因是因爲你使用的sfWidgetFormInputPassword部件。

最佳做法是永不預先填寫密碼字段,這就是sfWidgetFormInputPassword小部件爲您所做的。

如果你想違背最佳實踐,做到以下幾點,

$this->widgetSchema['passwordagain']->setOption('always_render_empty', false); 

不過,我認真建議你不這樣做。

+0

你說得對。當我想到更多關於它的時候,db中的密碼是md5,所以如果我使用那個值來渲染字段,它不會做任何好事。 感謝您的幫助。 – 2010-02-08 06:37:22

相關問題