如何为 symfony 表单中的额外字段设置默认值
我需要做的是通过添加“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的默认设置不起作用的原因是您正在使用
sfWidgetFormInputPassword
小部件。最佳实践是永远不要预先填写密码字段,这就是
sfWidgetFormInputPassword
小部件为您所做的。如果您想违背最佳实践,请执行以下操作,
但是,我强烈建议您不要这样做。
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,
However, I seriously recommend you don't do that.