This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Including changeable nested form depending on other field

This is for discussion, mainly on 1.x but there's some 2.x topics here too.

Including changeable nested form depending on other field

by BagoZonde » Thu Jul 24, 2014 9:26 pm


This is my first topic, so hello everyone! I'm a Symfony2 newbie however I'm learning fast so I hope I will be able to skip to next level and help others too :).

Ok, so, here we go with my question.

Is anybody know how to build Symfony 2 form (using Doctrine 2 and annotations) described as:

1. I have KingEntity which contains "type", "settings" and "somethingElse" fields. Field titled "settings" will contain serialized data as it can differ depending on "type" selected.
2. When type==1 I want get child-form (or something like that, maybe some sets of fields??) for QueenOne. And when type==2, I want get QueenTwo child-form/entity, and so on. Each Queen* form contains different fields that's why I want to serialize it and put just in one "settings" field. I don't want to create new tables in database and create relations between them at all.
3. So, if type==1 is selected, I want validate this form using QueenOne form. If validation success, I want to serialize that data from QueenOne and put into "settings" field of KingEntity, so KingEntity is keeped in database and it contains only "type", "settings" and "somethingElse" fields.
4. I'm not interested with viewing this form, I need only backend part of logic for this kind of form.

I hope I've explained it clearly. Any help will be appreciated.
Thanks in advance!
Junior Member
Posts: 2
Joined: Thu Jul 24, 2014 8:47 pm

Re: Including changeable nested form depending on other fiel

by BagoZonde » Mon Jul 28, 2014 10:24 am

Ok, I've finally managed how to resolve this problem. I've used validation_groups for particular group of fields. Particular validation group is selected in DefaultOptions of form depending on value from selector field. So, all fields related to one of groups are declared in one KingEntity and they not exists in database, only in entity. I've added also callbacks like preUpdate, prePersist and postLoad to serialize/unserialize them and put into "settings" field. These callback methods contains list of fields I want to add to serialize, however is it possible to add these fields using validation groups etc.?

I'm doing in that way (this method is called while prePersist / preUpdate of entity):

Code: Select all
        $settings = array();
        if($this->getType() == self::TYPE_1) {
            $settings = array(
                'field1' => $this->getField1(),
                'field2' => $this->getField2()
        } else if ($this->getType() == self::TYPE_2) {
            $settings = array(
                'field3' => $this->getField3(),
                'customized_array' => array(
                    'field4' => $this->getField4(),
                    'field5' => $this->getField5()

So, field1 & field2 are related to validation group "queen-one" and field2, field3 and field4 are related to validation group "queen-two".

My question is: is it possible to:
Get fields regarding to validation group (or JMS Serializer group if it's possible) so putting these fields to array would go automatically without listing each field as in above example. And the second question: All these fields are flattened in entity, however as you see, I need nested array 'customized_array' so I think I can't go without listing all that fields in that structure. However, if this array would be without nested arrays, is it possible to use some adding fields by group automatically etc. ?
Junior Member
Posts: 2
Joined: Thu Jul 24, 2014 8:47 pm