6

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

形式

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';
   }
}

控制器

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

3 回答 3

4

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

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 %}
于 2012-04-10T06:51:26.867 回答
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()));
于 2012-04-10T19:07:46.523 回答
1

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

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

于 2012-04-10T08:19:46.073 回答