在我的Form Symfony2中创建重复字段

时间:2012-04-10 05:50:07

标签: symfony

我正在开展一个大学项目,我想让所有学生参加。我创建了一个包含3个字段的模型,即日期,现在(布尔)和student_id。现在当我尝试从中生成一个表单时,它只显示这三个字段。但是我想要全班同学。所以我为学生创建了一个循环并创建了一系列考勤对象。现在我被卡住了,我不知道如何将它们传递给我的TWIG文件,如果这是正确的方法,我也很困惑。这是我的模型和控制器代码

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,
    ));
}

如何以一种单独的复选框向我显示所有学生的方式呈现表单?

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 %}

3 个答案:

答案 0 :(得分:3)

未经测试。但应该有一点修改。 控制器中的循环:

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

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

返回数组:

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

在模板中:

{% for sform in form %}
    {{ form_widget(sform) }}
{% endfor %}

答案 1 :(得分:3)

我想你可能走错了路。 S2已经开箱即用。

通读:

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

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

您基本上想要创建一个主ListofStudents表单,然后嵌入StudentAttendence表单。然后,您可以将学生数组传递给主表单,所有数组处理都会神奇地发生。

在ListOfStudents表单中,您将有类似的内容:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder->add('students', 'collection', array('type' => new StudentAttendenceType()));

答案 2 :(得分:1)

您将变量students返回到模板,并且您在树枝中调用student.firstname,它应该是students.firstname,并且您应该将'form' => $form->createView()保留在你的控制器。

我认为它应该更好用,希望这有帮助!