开发者

update/validate embedded forms before saving in symfony

I would like to validate an embedded form field before it gets saved in the database. Currently it will save an empty value into the database if the form field is empty. I'm allowing 开发者_JAVA百科empty fields, but I want nothing inserted if the form field is empty.

Also quick question, how to alter field values before validating/saving an embedded form field?

$this->form->getObject works in the action, but $this->embeddedForm->getObject says object not found


I found a really easy solution. The solution is to override your Form's model class save() method manually.

class SomeUserClass extends myUser {

public function save(Doctrine_Connection $conn = null)
    {
        $this->setFirstName(trim($this->getFirstName()));
        if($this->getFirstName())
        {
                return parent::save();
        }else
        {
                return null;
        }
    }

}

In this example, I'm checking if the firstname field is blank. If its not, then save the form. If its empty, then we don't call save on the form.

I haven't been able to get the validators to work properly, and this is an equally clean solution.


Symfony gives you a bunch of hooks into the form/object saving process. You can overwrite the blank values with null pre/post validation using by overriding the the doSave() or processValues() functions of the form.

You can read more about it here: http://www.symfony-project.org/more-with-symfony/1_4/en/06-Advanced-Forms#chapter_06_saving_object_forms

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜