Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Repeative Fields inside my Form Symfony2

Tags:

symfony

I am working on a college project where I want to take the attendance of all the students. I have created a model with 3 fields i,e date, present (boolean) and student_id. Now when I try to generate a form out of it, it will display me only these 3 fields. However I want all the students of the class. So I created a loop for students and created an array of attendance objects. Now I am stuck, I don't know how I can pass them to my TWIG file, and I am also confused if it ist the right way to do this. Here is my Model and Controller code

FORM

namespace College\StudentBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class StudentAttendanceType extends AbstractType
{
   public function buildForm(FormBuilder $builder, array $options)
   {
     $builder
        ->add('date')
        ->add('present')
    ;
   }

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

CONTROLLER

public function takeAttendanceAction($Department_Id)
{
    $students = $this->getDoctrine()
    ->getRepository('CollegeStudentBundle:Student')
    ->findAll($Department_Id);

    foreach($students as $key => $student){

        $attendance[$key] = new StudentAttendance();
        $form[$key] = $this->createForm(new StudentAttendanceType(), $attendance[$key]);
    }

    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {

        $form->bindRequest($request);
        if ($form->isValid()) {

            $em = $this->getDoctrine()
            ->getEntityManager();
            $em->persist($attendance);
            $em->flush();
            return $this->redirect($this->generateUrl('CollegeStudentBundle_index', array('id' => $Department_Id)));
        }
    }
    return $this->render('CollegeStudentBundle:StudentAttendance:take-attendance.html.twig', array(
            'form' => $form->createView(), 'department' => $Department_Id, 'students' => $students,
    ));
}

How can I render the form in a way that it will display me all the students with a separate checkbox ?

HTML.TWIG

{% block body  %}
<form  action="{{ path('CollegeStudentBundle_take_attendance',{'id':department} ) }}" method="post" {{ form_enctype(form) }} name="acadimics-form" id="acadimics-form" >

{{ form_errors(form) }}
{{ form_row(forms[0].date) }}

<table id="mytabs" border="1" cellpadding="5" cellspacing="2" width="100%" >
   <tr>
    <th> Enrolment No. </th>
    <th> Student's Name </th>
    <th> Present </th>
   </tr>
  {% for student in students %}
    <tr>
       <td> {{ student.enrolmentNo }} </td>
       <td> {{ student.firstname }} {{ student.lastname }} </td>
       <td> {{ form_row(form.present) }} </td>
        </tr>
  {% endfor %}
  {{ form_rest(form) }}
</table>

<input type="submit" value="Take Attendance"  />
</form>
  </div>

{% endblock %}
like image 806
ScoRpion Avatar asked Apr 10 '12 05:04

ScoRpion


3 Answers

Not tested. But there should be a little modification. The loop in the controller:

foreach($students as $key => $student){

    $attendance[$key] = new StudentAttendance();
    $form[$key] = $this->createForm(new StudentAttendanceType(), $attendance[$key])->createView();
}

return the array:

return $this->render('CollegeStudentBundle:StudentAttendance:take-attendance.html.twig', array(
    'form' => $form, 'department' => $Department_Id, 'students' => $students,
));

In the template:

{% for sform in form %}
    {{ form_widget(sform) }}
{% endfor %}
like image 153
seferov Avatar answered Sep 18 '22 11:09

seferov


I think you may be on the wrong track. S2 already deals with arrays out of the box.

Read through:

http://symfony.com/doc/current/cookbook/form/form_collections.html

http://symfony.com/doc/current/reference/forms/types/collection.html

You basically want to create a master ListofStudents form and then imbed a StudentAttendence form. You can then pass your array of students to the master form and all the array processing will happen magically.

In your ListOfStudents form you will have something like:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('students', 'collection', array('type' => new StudentAttendenceType()));
like image 36
Cerad Avatar answered Sep 21 '22 11:09

Cerad


You return the variable students to the template and you're calling student.firstname in twig, it should be students.firstname and you should keep your 'form' => $form->createView() in the return of your controller.

I think it should work better, hope this help!

like image 21
Snroki Avatar answered Sep 22 '22 11:09

Snroki