Search code examples
phpsymfony1doctrinesymfony-1.4doctrine-1.2

unset embeded forms in symfony


i have two embeded forms Form1 and Form2 in a general form class FormA, in the general class, besides the embeded forms i have a choice widget and in the doBind i try to unset one of the embeded forms (depending on the choice widget content) this does'nt WORK : Person and animal models inherit from creature(column agregation)

 protected function doBind(array $values)
        {
//embeded forms are 'person' and 'animal'
            $forms = $this->embeddedForms;


            if($values['type']== 'animal')
            {
                unset($forms['person']);

                unset($values['person']['last_name'], $values['person']['first_name'] , $values['person']['civility'], $values['person']['id'] );
            }
            parent::doBind($values);

        }

the form is never unseted and in the db i have 3 saves (one for animal, one for the person and one for the creature)

any idea guys???


Solution

  • Unsetting an embedded form after the configure() method is called is too late in the lifecycle of a form. Implement your configure() method so it only embeds those subforms you realy need.

    For example pass the value of the selected choice to the form and then decide in the configure() method whether to add the PersonForm or the AnimalForm.