symfony 2.3 form getData doesn't work in subforms collections

前端 未结 5 389
南方客
南方客 2021-01-31 23:04

I have a form which contains a collection. So I have:

/* my type */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
             


        
5条回答
  •  时光说笑
    2021-01-31 23:50

    In my case did not need the data necessarily when building the form, but when building the view (later). Just next to the buildForm function of my subform type class, I added the buildView function:

    namespace AppBundle\Form;
    
    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\Form\FormView;
    use Symfony\Component\Form\FormInterface;
    
    class MyType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            // ...
        }
    
        public function buildView(FormView $view, FormInterface $form, array $options)
        {
            $data = $form->getData();
            $view->vars['name'] = $data->objproporwhatever;
        }
    
        // ...
    }
    

    Because buildView is called later, the data is available there. In this example I used it to change the label of the form row of each item in the collection. Check out the list of possible vars.

提交回复
热议问题