Symfony Models normally implant a couple of a great many other forms, that is beneficial to revise related agencies in one single mode. On this page, you can easily perform an application so you can edit a role class and you may, correct in the same form, you can edit, manage and take off many Level items pertaining to you to Task.
How exactly to Embed a set of Versions
Next, let’s do an application for the task entity, playing with good CollectionType realm of TagType variations. This can allow us to modify all of the Mark parts of a job proper during the activity setting alone:
When the user submits the shape, the brand new submitted investigation to your labels industry is employed to create an ArrayCollection of Tag things. Brand new collection will then be intent on the fresh new tag world of the new Activity and will be utilized thru $task->getTags() .
Yet, that it is effective, however, in order to modify current tags. It generally does not are suffering from yet , to provide the fresh tags otherwise remove current ones.
You might implant nested choices as much profile down as you such. not, if you are using Xdebug, you may want to found a maximum function nesting number of ‚100‘ achieved, aborting! mistake. To resolve this, boost the xdebug.max_nesting_top PHP function, otherwise offer for each setting field yourself playing with mode_row() unlike helping to make the whole setting immediately (e.g form_widget(form) ).
Allowing „new“ Tags into „Prototype“
Before you additional two labels into task from the operator. Today let the pages put as many tag models as they you need in direct brand new web browser. This requires just a bit of JavaScript code.
But earliest, you will want to allow setting range know that as opposed to just two, it does located a telephone number from tags. Otherwise, you will notice a „This form shouldn’t incorporate additional industries“ mistake. This is accomplished to your enable it to be_create 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 form.tags.vars.prototype are a questionnaire element that looks and you can seems identical to the person mode_widget(mark.*) factors in your having cycle. Consequently you might call function_widget() , form_row() otherwise mode_label() on it. You can actually prefer to bring singular of its fields (elizabeth.grams. the name industry):
If you give the whole „tags“ sub-form at a time (e.g. form_row(setting.tags) ), the information-prototype attribute is automatically placed into the newest that contains div , and you ought to to alter the following JavaScript appropriately.
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
The addFormToCollection() function’s work is to use the research-prototype feature to help you dynamically put a unique form when this link try engaged. The knowledge-model HTML has got the tag’s text input feature with a reputation away from activity[tags][__name__][name] and you will id of activity_tags___title___term . Brand new __name__ is actually a great placeholder, which you can make up for a separate, incrementing amount (elizabeth.grams. task[tags][name] ):
Today, whenever a user presses the latest Include a tag hook up, an alternate sub form will appear on the web page. When the setting is actually submitted, people new level versions would be converted into the fresh new Tag items and you will put in the new tags possessions of Task target.
And work out approaching these types of the newest labels convenient, put an enthusiastic „adder“ and you will established men a great „remover“ way for new labels throughout the Task category:
With this a couple changes, if form is submitted, for each and every brand new Mark target is actually placed into work group because of the getting in touch with this new addTag() strategy. Before this transform, they were extra inside the house by the setting by getting in touch with $task->getTags()->add($tag) . That has been great, but pushing employing the new „adder“ means produces addressing this type of the brand new Tag items simpler (especially if you may be using Doctrine, which you will discover in the next!).