How to Implant a set of Models

How to Implant a set of Models

Symfony Forms can be implant a collection of many other variations, which is good for revise associated organizations in one single function. On this page, it is possible to manage a form to modify a task classification and you will, right inside exact same function, you’ll be able to modify, create and take off many Mark items connected with one to Task.

How-to Implant some Versions

Next, why don’t we carry out an application for the task entity, playing with a beneficial CollectionType arena of TagType models. This will allow us to personalize all of the Mark parts of a job proper within the activity mode by itself:

In the event that user submits the design, this new submitted studies to the labels industry is utilized to build a keen ArrayCollection out of Tag things. This new range will be seriously interested in the fresh new level realm of brand new Activity and can end up being reached via $task->getTags() .

So far, so it is effective, however, simply to modify existing labels. It will not have developed yet , to include the labels or erase current of these.

You could embed nested choices as numerous account off because you particularly. Yet not, instanthookups if you utilize Xdebug, it’s also possible to discovered a maximum mode nesting amount of ‘100’ attained, aborting! mistake. To solve this, improve the xdebug.max_nesting_level PHP setting, otherwise bring for each function job yourself having fun with function_row() as opposed to helping to make the entire mode at once (e.grams mode_widget(form) ).

Allowing “new” Tags to the “Prototype”

In past times your additional several labels for the task in the operator. Today let the pages put as much tag models while they you prefer directly in this new browser. This calls for a little bit of JavaScript password.

But very first, you really need to allow mode collection know that in the place of just several, it does discovered a phone number regarding tags. If not, you will see good “This type cannot contain a lot more areas” mistake. This is done for the create_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 shape.labels.vars.model try a type function that looks and you will seems same as anyone form_widget(mark.*) factors inside your to have loop. This means that you could label form_widget() , form_row() otherwise form_label() inside it. You could potentially actually love to promote only one of their fields (e.g. the name community):

    For those who provide the whole “tags” sub-mode at a time (age.grams. function_row(setting.tags) ), the content-model characteristic is actually immediately placed into the fresh new who has div , and you need to to switch the second JavaScript properly.

    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 newest addFormToCollection() function’s job is to try to make use of the research-prototype attribute to help you dynamically add a special function when this hook are visited. The information and knowledge-prototype HTML contains the tag’s text message input function that have a name regarding task[tags][__name__][name] and id from activity_tags___title___label . The fresh __name__ are a placeholder, that you’ll replace another, incrementing count (elizabeth.g. task[tags][name] ):

    Now, anytime a person ticks the brand new Include a tag hook up, a different sandwich setting will look to your web page. If the form is recorded, one brand new mark versions might be converted into the fresh new Tag things and you will put in new tags possessions of Activity target.

    And make dealing with these types of the fresh tags smoother, create a keen “adder” and you can a “remover” opportinity for brand new tags from the Task group:

    With our several transform, if the mode was submitted, for each the Level target was added to work classification by the contacting brand new addTag() approach. Until then transform, these people were extra in of the means by calling $task->getTags()->add($tag) . Which had been good, however, forcing employing brand new “adder” approach helps make addressing such this new Mark objects smoother (particularly if you’re using Doctrine, which you will learn about 2nd!).

Hai bisogno di aiuto?