Symfony2.3:表单事件中缺少未映射的字段

时间:2013-09-19 23:41:15

标签: php forms symfony

已在表单类型中添加了两个未映射的隐藏字段,以便在PRE_SUBMIT表单事件中启用数据修改。但是,在Netbeans调试中观察到的表单事件中不提供字段及其数据。有第三个未映射的字段可用。测试表明隐藏字段不是字段不出现在事件中的原因。隐藏字段在表单中正确呈现。奇怪的是,使用时

$form->getErrorsAsString();

在控制器中,字段出现在表单错误字符串中(因为没有错误)。

表格类:

class HouseholdType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('active', 'choice', array(
                'choices' => array('1' => 'Yes', '0' => 'No'),
                'data' => 1,
            ))
            ->add('addresses', 'collection', array(
                'type' => new AddressType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'prototype' => true,
                'prototype_name' => '__address__',
            ))
            ->add('arrivalmonth', new Type\MonthType(), array(
                'empty_value' => false,
                'data' => date('n'),
            ))
            ->add('arrivalyear', new Type\YearType(), array(
                'empty_value' => false,
            ))
            ->add('foodStamps', 'choice', array(
                'choices' => array('0' => 'No', '1' => 'Yes', '2' => 'Appl.'),
                'empty_value' => false,
            ))
            //this field appears at Form Event
            ->add('isHead', 'choice', array(
                'expanded' => true,
                'mapped' => false,
                    )
            )
            //the following two fields do not appear in Form Event
            ->add('headId', 'hidden', array(
                'mapped' => false,
            ))
            ->add('v1Date', 'hidden', array(
                'mapped' => false,
            ))
            ->add('members', 'collection', array(
                'type' => new MemberType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'prototype' => true,
            ))
            ->add('wic', 'choice', array(
                'choices' => array('0' => 'No', '1' => 'Yes', '2' => 'Appl.'),
                'empty_value' => false,
            ))
    ;

    $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
                $data = $event->getData();
                $event->setData($data);
            });
}

1 个答案:

答案 0 :(得分:0)

在模板中手动包含字段时,请使用formname [fieldname]。例如,未映射的隐藏字段isHead应该是: <input type="hidden" name="household[isHead]"...