如何为 symfony 表单中的额外字段设置默认值

发布于 2024-08-20 13:11:39 字数 965 浏览 7 评论 0原文

我需要做的是通过添加“passwordagain”字段来自定义我的默认用户表单,并将值设置为等于“password”字段的新字段。 我的代码:

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 方法似乎不起作用。也许它只适用于新用户,但这不是我在这里寻找的。

谢谢, 拉杜。

PS:顺便说一句...我使用 symfony 1.4 和 propel

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.
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. Perhaps it works only for new users, but that's not what i am looking for here.

Thanks,
Radu.

P.S.: btw... i use symfony 1.4 with propel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

毁我热情 2024-08-27 13:11:39

您的默认设置不起作用的原因是您正在使用 sfWidgetFormInputPassword 小部件。

最佳实践是永远不要预先填写密码字段,这就是 sfWidgetFormInputPassword 小部件为您所做的。

如果您想违背最佳实践,请执行以下操作,

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

但是,我强烈建议您不要这样做。

The reason your default isn't working is because you're using the sfWidgetFormInputPassword widget.

Best practice is to never pre-fill a password field, and this is what the sfWidgetFormInputPassword widget does for you.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文