1

Sorry for the generic title, but don't find any better one to describe my situation, that I describe next.

I'm working on a project that requires to create a Project that have various Subprojects associated. I created a Project entity and a Subproject one. Now, there are more than one kinds of subprojects but all of them share most of the properties, so I created the subproject entity in such a way that the differences are stored in a Options field. So, each project will have the common fields + an array of options in the options field.

When it comes to create the form to enter the data though, I want to render the independent fields that the user can fill out for each Subproject (it's only one big form with the main project and subprojects fields to fill).

Then, when the user submits the data, I want to take the fields that are "different" in each subproject and put them in the options field.

[EDIT]To make it more clear, I have a Project, and then a Platform 1 subproject, a platform 2 subproject and so on. Platform 1 subproject can have the common subproject fields + my_custom field 1; then Platform 2 subproject would have common fields + my_custom field 2. Now, when I will store the subprojects, I will put them in Subproject table, where I fill out the common fields and then the fields that difer (in concept, amount or whatever) I store in options field. Each subproject, then will have an options field with a particular formatted data on it. That been said, each form for each subproject will slightly difer from the rest.[EDIT]

This is the question then: How can I create such a form that includes forms for specific subprojects, and then transform those "different" fields into one only options field. Do I need to create a FormType for each subproject type? If yes, then do I need to create an Entity (not persistable) for each of those subprojects types?

Please, if you don't understand ask me and I will try to answer any question to clarify this. Don't include code because I didn't think is necessary, but if you think it is, please, let me know. I have read the DataTransformers section in Symfony book, and Embedded forms, so pointing in that direction will not help me that much. Nevertheless, if you have a practical insight on how can I use those things to achieve what I want, your advice is more than welcome.

Some questions in SO address this topic for simple fields, but don't provide the answers I'm looking for.

Any clue is appreciated. Thanks in advance.

1
  • The question was edited to show clearly (I hope) what I'm trying to say. Commented Nov 17, 2013 at 1:29

2 Answers 2

1

Start by creating a ProjectFormType along with a SubProjectFormType. Make sure you understand how to use the collection field type to allow one project to contain multiple sub projects.

http://symfony.com/doc/current/cookbook/form/form_collections.html

Once you have the basics down then read and understand how to dynamically add fields to a form.

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html

You will basically create a SubProjectSubscriber which listens to the preSetData event. In your SubProjectFormType you will add the subscriber.

The subscriber get's passed the actual instance of the subProject. The subscriber can then check the options and add custom form fields as needed.

But again, make sure you have the basics down before getting into the dynamic stuff.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks very much for answering. Dinamically adding fields to the form is not the issue here. My bad, I couldn't explain better. The issue here is to show more fileds in each subproject form than they are in the Entity, and then reconciling the "extra fields" in only one options field.
Ok, the collections field type "is used when you want to manage a collection of similar items in a form". While my items (subprojects) are similar when it comes to the way I store them, they are not similar in the form user enters the data, since I have to reflect their particularities (that in DB are mapped to only one field) rendering different forms.
I'm in the way to solve this. It will take far more than simply using Collections or dynamism, but certainly that's a key part. Thanks for your answers. I have to select one of the two answer (though both lead in the right direction). Beign that I'll use Event Subscribers, I select this. Thanks to both.
1

You can use the collection type in form builder to add new 'sub entities' directly from your Project form, you'll need a bit of javascript to append new 'sub entities' forms tho but prototype makes it pretty painless.

        ->add('sub_projects', 'collection', array('type' => new SubProjectType(),
                                          'allow_add'    => true,
                                          'allow_delete' => true,
                                          'prototype' => true))

I'll let you look up the actual code it is very well explained here http://symfony.com/doc/current/reference/forms/types/collection.html#basic-usage

To populate your array the best option is to use doctrine's prePersist event - especially if you are using 2.4 - but you can also add your logic before $form->isValid and $em->persist in the create/update methods of your controller class. Or also use JS to change the value of an hidden input on the fly but I really wouldn't recommend going that way.

3 Comments

Thanks for your answer. I'm reading through the appointed resource. Anyway, I'm not too worried about adding elements on the fly. The form can be layed out complete from the beginning. My concern is with presenting more fields in the subprojects, than the real fields in Entity, and then storing them in only one options field. That being said, I'm studying what you propose.
So ideally what you want is the ability for the user to add inputs which will be stored in an array property of a new SubProject Entity ? Or do you want predefined types the user chooses from ? Anyways you could do it with the above method and some extra JS to handle appending extra inputs on user choice / create.
I want to show as many forms as subproject types I have (it's fixed amount of types). Once the user filled them out, I want to store each subproject as a record in the same DB table, storing the common fields in their corresponding table columns, and store all those fields that are not common, in the respective "options" column. But certainly you and @Cerad pointed in the right direction. I'm working on it and if following that path I get what I need, then I will select the correct answer. Thanks a lot for your follow up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.