How to Embed a collection of Models

Symfony Models can implant some many other models, which is beneficial to revise relevant agencies in a single form. In this post, you are able to carry out a form in order to edit a job category and you can, best from inside the same means, possible modify, do and remove many Mark objects related to one Activity.

Ideas on how to Embed a couple of Variations

Second, why don’t we do a questionnaire for the task entity, playing with a CollectionType field of TagType variations. This may help us customize all of the Level elements of a task best inside task mode by itself:

In the event that affiliate submits the design, the fresh new filed studies into the labels occupation is employed to build an enthusiastic ArrayCollection from Tag objects. The fresh collection will then be set on the fresh new tag field of this new Task and will feel accessed via $task->getTags() .

Up to now, so it is effective, but simply to revise established tags. It will not have developed but really to include the new tags otherwise remove existing of these.

You might embed nested series as much levels off because you instance. not, if you use Xdebug, it’s also possible to discover a maximum function nesting number of ‘100’ achieved, aborting! error. To fix this, help the xdebug.max_nesting_height PHP function, otherwise provide for every single means field by hand having fun with function_row() in the place of rendering the whole means simultaneously (age.g function_widget(form) ).

Allowing «new» Tags with the «Prototype»

Prior to now you extra a few tags to the task about control. Today allow pages include as many level forms as they you desire directly in this new internet browser. This requires some JavaScript password.

However, earliest, you will want to let the form range know that instead of precisely several, it will discover an unknown number out of tags. If you don’t, you’ll see a great «This type ought not to contain more industries» mistake. This is done to the enable it to be_include choice:

The allow_add option also makes a prototype variable available to you. This «prototype» is a little «template» that contains all the HTML needed to dynamically create any new «tag» forms with JavaScript. To render the prototype, add the following data-prototype attribute to the existing

    in your template:

    The design.labels.vars.prototype try a questionnaire function that appears and you may seems same as anyone means_widget(tag.*) issues inside your having circle. This means that you could phone call mode_widget() , form_row() otherwise form_label() involved. You could also desire bring singular of its industries (age.g. title career):

    If you give the «tags» sub-function at the same time (age.g. setting_row(mode.tags) ), the information-model trait are automatically placed into the which includes div , and you should to alter another JavaScript consequently.

    Now add some JavaScript to read this attribute and dynamically add new tag forms when the user clicks the «Add a tag» link. Add a