两个
未映射的隐藏字段已添加到表单类型中,以启用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);
});
}
在模板中手动包含字段时,请使用 formname[字段名称]。 例如,未映射的隐藏字段是 head,应为: <input type="hidden" name="household[isHead]"...