1

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

stan
  • 4,885
  • 5
  • 49
  • 72

2 Answers2

1

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.

stan
  • 4,885
  • 5
  • 49
  • 72
0

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

xzyfer
  • 13,937
  • 5
  • 35
  • 46
  • I'm more interested in avoiding saving the form into the database than filling in something for the blank values. Is that possible? – stan Jan 05 '11 at 18:00
  • That should be simple. If you just call $form->isValid() is with validate without saving. Saving only occurs when you call $form->save() – xzyfer Jan 07 '11 at 00:14
  • I know that saving occurs when you call $this->form->save(); but my problem was that I actually didn't know when not to call that if the input was empty – stan Jan 07 '11 at 00:43