开发者

how to avoid storing several times a repeated field of a Symfony form?

开发者 https://www.devze.com 2023-02-03 21:30 出处:网络
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

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 h开发者_如何学Pythonave 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,


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号