6

私はすべての学生の出席を得たい大学のプロジェクトに取り組んでいます。date、present(boolean)、student_idの3つのフィールドを持つモデルを作成しました。これでフォームを生成しようとすると、これら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 に答える