A
A
Alexander2016-02-11 13:39:08
symfony
Alexander, 2016-02-11 13:39:08

How to make additional input fields in symfony?

There is symphony 3, there is a form.
There is one input field and a button for creating more input fields, which must have the same name as the first field in order to be stored in one cell in the database.
Is there a correct symfony-way for this case?
This is the form creation:

$form = $this->createFormBuilder($list)
            ->add('list_elements', TextType::class, array( 'allow_extra_fields' => true, 'attr' => array('class' => 'form-control', 'style' => 'margin-bottom:15px')))
            ->add('add_element', ButtonType::class, array('attr' => array('class' => 'form-control', 'onclick' => 'addInput(\'form\');' ,'style' => 'margin-bottom:15px')))
         ->add('save', SubmitType::class, array('label'=>'Create List','attr' => array('class' => 'btn btn-primary', 'style' => 'margin-bottom:15px')))
        ->getForm();

I have nothing special in twig:
{{form_start(form)}}
{{form_widget(form)}}
{{form_end(form)}}
+ js to create an input field with the same name( this is the wrong way)
---
when saving it gives:
This form should not contain extra fields.
---
Generally an essence in that to create the list (to 10 elements), with a name and to save.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Romanenko, 2016-02-11
@ghost1k

To build a form (twig + forms) symfony.com/doc/current/cookbook/form/form_collect... +
To transform data into 1 field: symfony.com/doc/current/cookbook/form/data_transfo...

E
Evgeny Svirsky, 2016-02-11
@e_svirsky

Such inputs should not be stored in one cell. Make a OneToMany relation and attach them to the form with collection. The data will be atomic and everything is as it should be :)

A
Andrey, 2016-02-11
@VladimirAndreev

m.b. list_elements[] will work?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question