1
votes

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 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

2

2 Answers

1
votes

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.

0
votes

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