【问题标题】:how to avoid storing several times a repeated field of a Symfony form?如何避免多次存储 Symfony 表单的重复字段?
【发布时间】:2011-01-11 17:49:21
【问题描述】:
我正在使用 Symfony 1.4 和 Doctrine。我有一个带有电子邮件字段的模型 A。 A 的形式显示用户应正确插入电子邮件的输入。但众所周知,有时他们不这样做。
为了解决这个问题,我在模型(和表单)中插入了一个额外的字段,称为 *repeat_email* 以防止拼写错误。然后,在验证过程中,验证所有字段后,我使用全局验证器来比较两个字段的数据。
这可行,但我不想让电子邮件在数据库中存储两次(我不想要 *repeat_email*)。有没有什么机制可以在验证过程中使用,但不存储在数据库中?
谢谢,
【问题讨论】:
标签:
symfony1
doctrine
symfony-forms
【解决方案1】:
从模型架构中删除 repeat_email 字段,并像这样配置您的表单:
//email widget and validator are configured in the base class
$this->widgetSchema['repeat_email'] = new sfWidgetFormInput();
$this->validatorSchema['repeat_email'] = clone $this->validatorSchema['email'];
$this->widgetSchema->moveField('repeat_email', 'after', 'email');
$this->mergePostValidator(new sfValidatorSchemaCompare('email', sfValidatorSchemaCompare::EQUAL, 'repeat_email', array(), array('invalid' => "Emails don't match")));
【解决方案2】:
错误可能是在模型中添加了一个额外的字段。您只需要在表单中。