0

Right now I'm rendering two forms ( one for company and one for it's tags ) and it looks like this:

<h3>Company</h3>
{{ form_start(form) }}
{{ form_row(form.name) }}
{{ form_row(form.city) }}
{{ form_row(form.street) }}
{{ form_row(form.postalcode) }}
{{ form_row(form.buildingnumber) }}
{{ form_row(form.vatid) }}
{{ form_row(form.tags) }} 
 <button id="test">Test</button>
{{ form_row(tags_form.title) }} 
{{ form_row(form.save) }}
{{ form_end(form) }}

I want users to be able to add another input ( after clicking test button ) {{ form_row(form.tags) }} so they can add multiple tags with one form, but right now my tags form looks like this:

    $builder
        ->add('title',null,array(
            'label' => 'tag.title',
            'required' => false
        ));

and I don't really know how to set this up. I tried with the simplest solution:

$('#test').on('click',function(e) {
        e.preventDefault();
        $('#fourcreate_portalbundle_companytags_title').clone().appendTo('form');
});

but that way submitting form creates entity only from the second input.

EDIT: I forgot to add - it has to be done with two forms, because first form contains list of currently available tags and the second form is to let users add their own.

2 Answers 2

1

You should not use two forms, but have a collection of tags_form in form.tags. The sample in the Cookbook is about adding tags.

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

4 Comments

Ahhh I forgot to add - it has to be with two forms, because the first form contains list of currently available tags, and the second form is to let users add their own tags.
Come on, you really should read the link I gave. It not only teaches handy (and very unintuitive) stuff about forms, but also has the 'Allowing "new" tags with the "prototype"' paragraph, which is exactly what you want. At the end of the cookbook you will know how to create a control like the one here on SO for tagging: you type, then either pick from a list, or add what you have typed as a new item, and then continue typing/selecting the next one.
yeah, ok, I can allow user to add and delete those tags but I can't manage to combine adding/deleting with choosing from already existing tags.
"I can't manage to combine" - what does this refer to? A) Is there a specification restriction imposed on the UI and so you're not allowed to do it, or B) is there already some code in your backend that prevents you from achieving this functionality or C) you've found the docs on how to do it but it still does not work. In any of the cases we need additional info on your current code to be able to help.
0

The browser sends a "clobbered" form where two or more name attribute of two or more input elements have the same value. Hence, the back-end gets only the last value. I can not be more specific because I am not familiar with that part of Symfony.

If you wish to clone input elements, and have their values submitted correctly, you must at least modify the name attributes before submitting the form. Also, watch out for non-unique id attributes, as that violates the HTML (DOM?) standard.

e.g

var clone = $('input[id="original"]').clone();
clone.attr('name', clone.attr('name') + '1');

Comments

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.