ramiromd
ramiromd

Reputation: 2029

How render a form many times

im developing the create student requiriment for my application. I have my Student entity, the entity contains two properties:

  1. User (instance of User entity)
  2. Course (instance of Course entity)

I build the form, but i wish render the form same times via a click button. On this way the administrator could be add any students without refresh the page.

Its is possible ? How manage the submit on the controller ?

Any ideas ? Thanks

NOTE: Im search a similiar Phpmyadmin behavior when add a new record.

Upvotes: 1

Views: 132

Answers (1)

antony
antony

Reputation: 2893

What you should do is create a new object and form (e.g. StudentCollection) that allows for adding the student form using the collection type. This will allow you to manage the dynamically adding/removing student forms a lot better.

More on form collections here http://symfony.com/doc/current/cookbook/form/form_collections.html

e.g. Assuming you have a student form called StudentFormType, something like this should work. There's a good example on the link above that you should use if you want to know how to dynamically add/remove student forms as well as handle the submission.

// src/PathTo/YourBundle/Form/Type/StudentCollectionFormType.php

// Form object
class StudentCollectionFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('students', 'collection', array(
                'type' => new StudentFormType(),
                'allow_add' => true,
                'allow_delete' => true,
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'PathTo\YourBundle\Model\StudentCollection',
        ));
    }

    // ...
}



// src/PathTo/YourBundle/Model/StudentCollection.php
namespace PathTo\YourBundle\Model;

// ...
class StudentCollection
{
    // ...
    protected $students;

    public function __construct()
    {
        $this->students = new \Doctrine\Common\Collections\ArrayCollection();
    }

    public function getStudents()
    {
        return $this->students;
    }

    public function addStudent(Student $student)
    {
        $this->students->add($student);
    }

    public function removeStudent(Student $student)
    {
        $this->students->removeElement($student);
    }
}

Then in your controller

// src/PathTo/YourBundle/Controller/StudentController.php
public function editAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $collection = new StudentCollection();

    // Prepopulate the collection with students
    // ...

    $form = $this->createForm(new StudentCollectionFormType(), $collection);

    $form->handleRequest($request);

    if ($form->isValid()) {
        foreach ($collection->getStudents() as $student) {
            $em->persist($student);
        }

        $em->flush();

        // redirect back to some edit page
        // ...
    }

    // render some form template
    // ...
}

Upvotes: 3

Related Questions