How exactly to Embed a collection of Models

How exactly to Embed a collection of Models

Symfony Variations normally embed a couple of a number of other variations, which is advantageous to edit relevant organizations in one single function. On this page, it is possible to manage a type in order to modify a task group and you can, best in the exact same mode, you’ll be able to revise, manage and take away many Level things connected with one to Activity.

Simple tips to Embed a collection of Variations

2nd, let us do a questionnaire to do the job organization, having fun with a good CollectionType world of TagType versions. This may allow us to modify every Level areas of a task right when you look at the activity mode itself:

In the event the affiliate submits the design, new submitted studies towards the labels occupation can be used to create an ArrayCollection away from Tag objects. The fresh collection will then be intent on the fresh level arena of the new Activity and will getting utilized via $task->getTags() .

Up to now, that it works great, but in order to change existing tags. It generally does not are suffering from but really to incorporate the new tags otherwise remove existing ones.

You could potentially embed nested collections as much membership off as you instance. But not, when you use Xdebug, you may located a max form nesting quantity of ‘100’ hit, aborting! error. To solve so it, improve the xdebug.max_nesting_top PHP means, or bring per setting industry yourself playing with setting_row() in lieu of leaving the complete setting immediately (elizabeth.g mode_widget(form) ).

Allowing “new” Tags on the “Prototype”

Prior to now you additional one or two labels to your task throughout the control. Today allow the profiles add as many tag models because they need in direct the newest internet browser. This involves just a bit of JavaScript code.

However, basic, you should allow the setting range be aware that in place of exactly several, it can receive an unknown number out-of labels. If not, you will see a good “This type cannot contain more industries” error. This is accomplished towards succeed_add alternative:

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.tags.vars.prototype are a questionnaire element that looks and you can feels same as anyone setting_widget(level.*) issue within your to possess loop. As a result you can phone call means_widget() , form_row() or form_label() involved. You might also like to promote singular of the fields (age.g. the name occupation):

    For individuals who offer the “tags” sub-function immediately (elizabeth.grams. means_row(form.tags) ), the data-prototype attribute is immediately placed into the fresh new with div , and you ought to adjust another JavaScript correctly.

    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 tag somewhere on your page to include the required functionality with JavaScript:

    The addFormToCollection() function’s business is to try to utilize the study-prototype attribute to dynamically create yet another setting when this hook up are engaged. The info-model HTML has the tag’s text input function having a name off task[tags][__name__][name] and id out of activity_tags___label___identity . The latest __name__ is actually good placeholder, which you’ll replace with a special, incrementing amount (elizabeth.grams. task[tags][name] ):

    Now, each time a user ticks the newest Put a tag link, a different sub mode look to your page. If function was submitted, one the new tag versions would be changed into the latest Tag objects and you can put into the brand new tags property of your Activity object.

    Making approaching these the brand new labels simpler, include a keen “adder” and you will an excellent “remover” way for the brand new tags on the Activity classification:

    With this one or two transform, in the event the mode are submitted, each new Tag object is actually set in the work classification from the contacting the latest addTag() strategy. Until then alter, these were added internally by the mode by getting in touch with $task->getTags()->add($tag) . That has been fine, but forcing the usage the fresh new “adder” approach produces handling these types of the fresh Tag things smoother (particularly if you datingranking.net/tr/swingstown-inceleme/ might be playing with Doctrine, that you will discover on 2nd!).

Leave a Reply

Your email address will not be published. Required fields are marked *