使用Symfony 2中的表单生成器更新多个记录

时间:2013-09-19 12:06:51

标签: forms symfony

我有一个用户需要编辑选项的记录列表。这是我到目前为止所得到的:

{% for mpt in mempassedtest %}
    <!-- Modal Windows: Add Tests & Achievements -->
    <div id="editTA{{mpt.id}}" class="modal fade">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4>Edit Test & Achievement</h4>
                </div>
                <div class="modal-body">
                    <form action="{{ path('members_edit_personaldetails', {'member' :memid}) }}" method="post" {{ form_enctype(ta) }} id="editFamilyDetails" class="modaledit">
                    <table class="table modalform table-condensed">
                        <tbody>
                            <tr class="hidden">
                                <th>{{ form_label(ta.id, 'ID*', { 'attr': {'class': 'title'} }) }}</th>
                                <td>
                                    {{ form_errors(ta.id) }}
                                    {{ form_widget(ta.id, { 'attr': {'class': 'form-control'}}) }}
                                </td>
                            </tr>
                            <tr class="hidden">
                                <th>{{ form_label(ta.contact, 'Member*', { 'attr': {'class': 'title'} }) }}</th>
                                <td>
                                    {{ form_errors(ta.contact) }}
                                    {{ form_widget(ta.contact, { 'attr': {'class': 'form-control'}}) }}
                                </td>
                            </tr>
                            <tr>
                                <th>{{ form_label(ta.testDescription, 'Test*', { 'attr': {'class': 'title'} }) }}</th>
                                <td>
                                    {{ form_errors(ta.testDescription) }}
                                    {{ form_widget(ta.testDescription, { 'attr': {'class': 'form-control'}}) }}
                                </td>
                            </tr>
                            <tr>
                                <th>{{ form_label(ta.taken, 'Test Taken*', { 'attr': {'class': 'title'} }) }}</th>
                                <td>
                                    {{ form_errors(ta.taken) }}
                                    {{ form_widget(ta.taken, { 'attr': {'class': 'form-control'}}) }}
                                </td>
                            </tr>
                            <tr>
                                <th>{{ form_label(ta.result, 'Result*', { 'attr': {'class': 'title'} }) }}</th>
                                <td>
                                    {{ form_errors(ta.result) }}
                                    {{ form_widget(ta.result, { 'attr': {'class': 'form-control'}}) }}
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <input type="submit" value="Save Changes" id="savebuttonta" class="btn btn-success" />
                    <a href="#" class="btn btn-danger" data-dismiss="modal">Close</a>
                    {{ form_rest(ta) }}
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endfor %}

在控制器中我有这个:

        // Retrieve Passed Tests For That Member
        $passedmembertests = $dm->createQuery('
        SELECT mt.id, mt.taken, mt.result, mtd.test
        FROM InstructorBundle:MapTests mt
        LEFT JOIN InstructorBundle:MapTestDescriptions mtd WHERE mtd.id = mt.testDescription
        LEFT JOIN InstructorBundle:MapCentreContacts mc WHERE mc.id = mt.contact
        WHERE mc.id = :member
        AND mt.result = :passed'
        )->setParameters(array(
                    'member' => $memberint,
                    'passed' => '1'
        ));

        $mempassedtest = $passedmembertests->getResult();

// Get Tests from Entity for Form use
$memberTA = $dm->getRepository('InstructorBundle:MapTests')->find($memberint);

// Generate Form to edit Tests & Achievements
$ta = $this->createForm( new TaType(), $memberTA);

但是,每次检索记录时都会生成模态窗口,但不会显示表单。我错过了什么?

修改
这是TaType()代码:

<?php
// src/Acme/MembersBundle/Form/Type/TaType.php
// This is to handle forms for the Members Form
namespace Acme\MembersBundle\Form;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('id', 'integer', array('required'=>false));
        $builder->add('contact','entity', array('class'=>'Acme\InstructorBundle\Entity\MapCentreContacts', 'property'=>'id'));
        $builder->add('testDescription','entity', array('class'=>'Acme\InstructorBundle\Entity\MapTestDescriptions', 'property'=>'test',
                                                        'query_builder' => function(EntityRepository $br) {
                                                            return $br->createQueryBuilder('mtd')
                                                                ->where('mtd.active = :active')
                                                                ->setParameter('active', '1');
                                                        }));
        $builder->add('taken', 'date', array('years' => range(1998, date('Y')), 'format' => 'dd-MMMM-yyyy'));
        $builder->add('result', 'choice', array(
            'choices'   => array(
                '0' => 'Failed',
                '1' => 'Passed'
            )
        ));
    }

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'      => 'Acme\InstructorBundle\Entity\MapTests',
            'csrf_protection' => false,
            'csrf_field_name' => '_token',
            // a unique key to help generate the secret token
            'intention'       => 'task_item',
        ));
    }

}

1 个答案:

答案 0 :(得分:0)

表单只能显示一次。

因为我看到你有相同的形式,并试图多次显示它(它在一个for)。 检查您是否第一次显示其工作的字段,以及您尝试显示它的其他时间,它不应返回任何代码。

更新: TAType()的多种形式 创建一个MainType并在里面添加这个

    ->add("taList","collection",
        array(
            "type"      => new TAType(),
            "label"     => false,
            'options'   => array(
                "label"     =>false,
            )
        )
    )