如何避免多次存储 Symfony 表单的重复字段?

发布于 2024-10-11 15:10:34 字数 295 浏览 3 评论 0原文

我正在使用 Symfony 1.4 和 Doctrine。我有一个型号 A,带有电子邮件字段。 A 的形式显示一个输入,用户应在其中正确插入电子邮件。但众所周知,有时他们不这样做。

为了解决这个问题,我在模型(和表单)中插入了一个名为 *repeat_email* 的额外字段,以防止拼写错误。然后,在验证过程中,验证所有字段后,我使用全局验证器来比较两个字段的数据。

这可行,但我不想将电子邮件在数据库中存储两次(我不想要 *repeat_email*)。是否有任何机制可以在验证过程中使用它,但不将其存储在数据库中?

谢谢,

I am working with Symfony 1.4 and Doctrine. I have a model A with an email field. The form of A displays an input in which the user should insert the email correctly. But as everybody knows, sometimes they don't do it.

To fix this I have inserted an extra field in the model (and in the form), called *repeat_email* to prevent the misspellings. Then, in the validation process, after validating all the fields, i use a global validator to compare the data of the two fields.

This works, but I don't want to have the email stored two times in the database (I don't want the *repeat_email*). Is there any mechanism to use it in the validation process, but not to store it in the database?

Thanks,

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

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

发布评论

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

评论(2

枕头说它不想醒 2024-10-18 15:10:34

从模型架构中删除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")));

Remove repeat_email field from the model schema, and configure your form like this:

    //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")));
思念满溢 2024-10-18 15:10:34

错误可能是在模型中添加了额外的字段。您只需要以表格形式提供。

the mistake was probably to add an additional field in the model. You only need it in the form.

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