0

Is there any way to adding element to a collection field in a form in the PRE_SUBMIT eventListner?

I have this (form1):

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('file',
              'file',
              array(
                    'data_class' => null,
                    'mapped' => false, //il campo non è mappato (il file non è blob ma sta su filesistem)
                    //'constraints' => new NotBlank(),
                    'render_required_asterisk' => true,
              )

        )
        ->add('name',
              'text',
               array(
                    'label' => 'Nome documento',
                    'required' => true,
                    'render_required_asterisk' => true,
                    'mapped' => 'name'
               )
        )
        ->add('document_category',
              'entity',
               array(
                'property' => 'name', //valore da visualizzare nel form 
                'mapped' => 'document_category',
                'class' => 'docliteBundle:Document\documentCategory', //Classe da dove deve prendere i dati
                'multiple' => false, //E' possibile la selezione di un solo campo 
                'expanded' => false, //Per rendere un campo select
                'empty_value' => 'Selezionare una categoria documento', //questo sarà impostato all'inizio ed identifica la voce vuoto
                'label' => 'Categoria documento', //label del campo
                'required' => true, //la selezione del campo è obbligatoria
                'render_required_asterisk' => true, //visualizza asterisco di campo obbligatorio
               )
        )
        ->add('attributes_rel',
                'collection',
                array(
                        'type' => new documentAttributeRelType(),
                        'label' => false,
                        'mapped' => 'attributes_rel',
                        'allow_add' => true,
                        'show_legend' => false,
                        'allow_delete' => true,
                )
        );

the documentAttributeRelType (form2):

$builder
        ->add('dynamic_attribute',
              'entity',
               array(
                    'property' => 'name',
                    'class' => 'docliteBundle:Document\dynamicAttribute',
                    'mapped' => 'dynamic_attribute',
                    'multiple' => false,
                    'expanded' => false,
                    'label' => 'Attributo',
                    'required' => true,
                    'constraints' => new NotBlank(),
               )
        )
        ->add('value',
              'choice',
              array(
                'mapped' => 'value',
              )
        )
    ;

In the Form1 is defined the Listner:

 $builder->get('attributes_rel')->addEventListener(
            FormEvents::PRE_SUBMIT,
            function(FormEvent $event){
                $data = $event->getData();

                $rel = array();
                $rel['dynamic_attribute'] = '1';
                array_push($data, $rel);
                $event->setData($data);
            });

But this adding element is not mapped and not appear in the view:

    {% for attribute in form.attributes_rel %}
        <div data-attribute_row>
            {{ form_row(attribute.dynamic_attribute) }}
            {{ form_row(attribute.value) }}
        </div>
    {% endfor %}
3
  • Yes, and as you already seem to know that you need the PRE_SUBMIT event, I guess that you're actually stuck with something more specific? Read symfony.com/doc/current/components/form/… And for a more practical aproach, look into this: symfony.com/doc/current/cookbook/form/… And if you still have problems, please be more specific in your question. Commented Jan 16, 2015 at 10:19
  • exactly , the form has a field collection consists of an entity and a value . Pre_submit event would add an element to this collection . As you can see in the example below, if you manually add the element in the array that happens : PRE_SUBMIT event is correctly added the third element SUBMIT event is not mapped to the third element , therefore I can not see it in the form . I do not know if I was clear . Ask me if you ulterirori doubts . Thank you for availability. Commented Jan 16, 2015 at 10:25
  • Finally I solve it!!!!! To set fileld collection in eventLister, the data must be set in PRE_SUBMIT but not in entity, directly in view with: $builder->addEventListener( FormEvents::PRE_SUBMIT, function(FormEvent $event){ $d = $event->getData(); $d['attributes_rel'] = array( array('dynamic_attribute'=>2), array('dynamic_attribute'=>3), array('dynamic_attribute'=>1), ); $event->setData($d); }); Commented Jan 16, 2015 at 17:19

1 Answer 1

1

I think you can find this useful for your situation, how to manipulate field options on PRE_SUBMIT event:

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
    $data = $event->getData();
    $form = $event->getForm();
    $pageId = $data['page_id'];

    $newOptions = $this->myManager->getPages();

    // change form field
    $form->add('page_id', 'choice', array(
        'label'         => 'Select a page',
        'choices'       => $newOptions,
        'required'      => true,
        'placeholder'   => 'Select a page',
        'attr'          => array('class' => 'form-control')
    ));

    $form->getData()->setPageId($pageId);
});
Sign up to request clarification or add additional context in comments.

3 Comments

thanks but in my case I have a collection field already set in the form . Should I add an item to this collection dynamically without using any button in the client. Thanks I
Use POST_SET_DATA event to add new item after the default ones were already set
I'm sorry, but I need to add this new item in one of PRE_SUBMIT, SUBMIT or POST_SUBMIT because depends from the document_category field. In my view i have already made the ajax call when category value changed but if I change any value for the entity in POST_SUBMIT this changed value not appear in view.

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.