dongshushen4392 2012-12-29 22:03
浏览 12
已采纳

动态字段是额外的?

I have defined an EventSubscriber with two possibilities, one of which adds a radio button with a variable number of buttons. All appears to behave as expected unless one of the radio buttons is checked. When checked, I get the

This form should not contain extra fields.

error.

How can this be avoided?

Some code:

Subscriber

class AddV2FieldsSubscriber implements EventSubscriberInterface {

    private $factory;

    public function __construct(FormFactoryInterface $factory) {
        $this->factory = $factory;
    }

    public static function getSubscribedEvents() {
        return array(FormEvents::PRE_SET_DATA => 'preSetData');
    }

    public function preSetData(DataEvent $event) {
        $data = $event->getData();
        $form = $event->getForm();
        if (null === $data) {
            return;
        }
        // check if the client object is v1: id exists, dateAdded is null
        $n = $data->getMembers()->count();
        if (!$data->getId() || $data->getDateAdded()) {
            $date = new \DateTime();
            $form->add($this->factory->createNamed('dateAdded', 'date', $date, array(
                    'widget' => 'choice',
                    'format' => 'MM/dd/yyyy',
                    'pattern' => '{{ year }}-{{ month }}-{{ day }}',
                    'years' => range(Date('Y'), Date('Y') - 5),
                    'required' => false,
                    'input' => 'datetime',
                ))
                    );
            $form->add($this->factory->createNamed('active', 'hidden', 'Yes')
                    );
            $form->add($this->factory->createNamed('updated', 'hidden', 'No')
                    );
            $form->add($this->factory->createNamed('sex', 'choice', null, array(
                    'choices' => array('Male' => 'Male', 'Female' => 'Female'),
                    'empty_value' => "Select a gender",
                    'required' => false))
                    );
            $form->add($this->factory->createNamed('dob', 'dob_age', null)
                    );
        } 
        elseif ($n && !$data->getDateAdded())
        {
            $members = $data->getMembers();
            // build choices array with member ids
            $choices = array();
            foreach ($members as $member) {
                $choices[$member->getId()] = 'Yes';
            }
            $form->add($this->factory->createNamed('isClient', 'choice', null, array(
                        'choices' => $choices,
                        'empty_value' => false,
                        'required' => false,
                        'mapped' => false,
                        'expanded' => true,
                        'label' => 'isClient',
                    )));
        }
   }
}
  • 写回答

1条回答 默认 最新

  • douzhan8652 2013-01-14 01:48
    关注

    The answer, in this case, was to use the debugger to find out what Symfony determined were extra fields. Turned out there were some hard-wired hidden fields in the template that needed to be removed. Doh!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并