0

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

$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);
            });
}
4

1 回答 1

0

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

于 2013-09-25T13:13:25.950 回答