2
votes

I have an entity "ticket" and I would like to create a form with many of this entity.

So I created a FormType like this :

class TicketType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('quantity');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => null,
        ));
    }

    public function getName()
    {
        return 'event';
    }
}

And here my controller :

    $clientTickets = array();

    for ($i = 0; $i < 5; $i++) {
        clientTickets[] = new Ticket();
    }

        $formTickets = $this->createForm(new TicketType(), array('tickets' => $clientTickets));

        if ($request->getMethod() == 'POST') {
            $formTickets->handleRequest($request);
            for ($i=0; $i < $formTickets.size(); $i++) { 

                $em->persist($ticket[$i]);
             }
        }

And finally my twig :

{{ form_start(form, {'attr': {'class': 'form-horizontal', 'id': 'msform', 'name': 'myform', 'method': 'POST' }}) }}

                                          {% for clientTicket in form.tickets %}

                                            {{ form_widget(form.quantity) }}

                                          {% endfor %}

                                              {{ form_rest(form) }}

                                          {{ form_end(form) }}

When I go on the page, I get this error :

Method "tickets" for object "Symfony\Component\Form\FormView" does not exist in YourBundle:folder:view.html.twi at line

Why did I do wrong ? Have you got a solution ?

2

2 Answers

3
votes

You may have to do something like this:

Let's have this form type:

class TicketType extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options) {
        $builder - > add('field1', 'text');
        $builder - > add('field2', 'text');
        $builder - > add('field3', 'text');
    }
    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver - > setDefaults(array(
            'data_class' = > null,
        ));
    }
    public function getName() {
        return 'event';
    }
}

Then in your controller you will need to have something like this:

public function testAction( )
{
    $clientTickets = array(
        'tickets' => array(
            array(
                'field1' => 'aaa',
                'field2' => 'aaa',
                'field3' => 'aaa'
            ),
            array(
                'field1' => 'bbb',
                'field2' => 'bbb',
                'field3' => 'bbb'
            ),
            array(
                'field1' => 'ccc',
                'field2' => 'ccc',
                'field3' => 'ccc'
            )
        )
    );

    $formTickets = $this->createFormBuilder( $clientTickets )
                        ->add( 'tickets', 'collection', array( 'type' => new TicketType() ) )
                        ->getForm();

    if ($formTickets->handleRequest( $this->getRequest() )->isValid()) {
        $data = $formTickets->getData();

        var_dump( $data );
    }

    return array(
        'form' => $formTickets->createView()
    );
}

And your twig will look something like this:

{{ form_start(form) }}

<ul>
    {% for clientTicket in form.tickets %}
        <li>{{ form_row(clientTicket.field1) }} {{ form_row(clientTicket.field2) }} {{ form_row(clientTicket.field3) }}</li>
    {% endfor %}
</ul>

{{ form_rest(form) }}
{{ form_end(form) }}

I think this code is self explanatory. Hope it helps

0
votes

UPDATE: I think you need to do something like this:

{% for i in 0..form|length %}
    {{ form[i].method }}
{% endfor %}