简体   繁体   English

如何以symfony形式将默认值设置为其他字段

[英]How to set default value to a extra field in a symfony form

What i need to do is customize my default userForm by adding a 'passwordagain' field and set the value to the new field equal to the 'password' field. 我需要做的是通过添加“再次密码”字段来自定义我的默认userForm并将值设置为等于“密码”字段的新字段。 My code: 我的代码:

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

} } }}

The setDefault method there doesn't seem to work. 那里的setDefault方法似乎不起作用。 Perhaps it works only for new users, but that's not what i am looking for here. 也许它仅适用于新用户,但这不是我在这里寻找的。

Thanks, Radu. 谢谢,拉杜

PS: btw... i use symfony 1.4 with propel PS:顺便说一句...我将symfony 1.4与propel一起使用

The reason your default isn't working is because you're using the sfWidgetFormInputPassword widget. 您的默认设置不起作用的原因是因为您使用的是sfWidgetFormInputPassword小部件。

Best practice is to never pre-fill a password field, and this is what the sfWidgetFormInputPassword widget does for you. 最佳实践是永远不要预先填写密码字段,这就是sfWidgetFormInputPassword小部件为您完成的工作。

If you want to go against best practice, do the following, 如果您想违背最佳做法,请执行以下操作,

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

However, I seriously recommend you don't do that. 但是,我强烈建议您不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM