Symfony2无效的动态表单没有错误

TiP*_*iPi 5 php forms dynamic symfony

我在symfony2上遇到了动态表单的问题.我正在尝试为提交的表单生成一些字段.换句话说,用户输入一些值,提交表单,并根据这些值,我的动态字段被添加到同一个表单(显然,第二次显示).为此,我在烹饪书中使用了这个例子:http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

所以,这是我的FormationType类

class FormationType extends AbstractType
{

private $em;
private $context;

public function __construct($em, $context) {
    $this->em = $em;
    $this->context = $context;
}

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name')
        ->add('date')
        ->add('type', 'choice', array(
            'mapped' => false,
            'choices' => Formationlist::getTypeTypes(false),
            'empty_value' => false,
        ))
        ->add('cost')
        ->add('travelCost')
        ->add('maximum')
        ->add('location')
        ->add('schedule')
    ;

    $formModifier = function(FormInterface $form, $type) {
        $formationList = $this->em->getRepository('CoreBundle:FormationList')->findBy(array("year" => 1, "type" => $type));

        $form->add('formationList', 'entity', array(
            'label'=> 'Titre formation', 
            'choices' => $formationList, 
            'class' => 'CoreBundle:FormationList', 
            'property' => 'title',)
                );

        };


    $builder->addEventListener(
        FormEvents::PRE_SET_DATA,
        function(FormEvent $event) use ($formModifier) {

            $data = $event->getForm();
            $type = $data->get('type')->getData();

            $formModifier($event->getForm(), $type);

        }
    );

    $builder->get('type')->addEventListener(
        FormEvents::POST_SUBMIT,
        function(FormEvent $event) use ($formModifier) {

            $type = $event->getForm()->getData();
            $formModifier($event->getForm()->getParent(), $type);
        }
    );
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'EXAMPLE\CoreBundle\Entity\Formation'
    ));
}

public function getName()
{
    return 'example_corebundle_formationtype';
}
}
Run Code Online (Sandbox Code Playgroud)

所以,两个addEventListener工作得很好.第一次显示我的表单时,未按预期加载formModifier中的字段.我的控制器类是以下一个:

public function createAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $contextSrv = $this->get('example.service.context');
    $context = $contextSrv->getContext();

    $entity  = new Formation();
    $form = $this->createForm(new FormationType($em, $context), $entity);
    $form->bind($request);

    if ($form->isValid()) {

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('formation_show', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form'   => $form->createView(),
    );
}
Run Code Online (Sandbox Code Playgroud)

由于我的一个动态字段不能为null,因此第一次提交表单时,它无效.因此,第二次加载FormationType.这意味着,如果填写了字段"type",我的formModifier()函数可以加载动态字段(formationList).直到那里,一切都很好,我得到了我的新领域.

但是,在表格上第二次"提交"之后......没有任何反应.页面刚刚重新加载,不会显示任何错误.

我检查了表单内容

var_dump($request->request->get('example_corebundle_formationtype'));
Run Code Online (Sandbox Code Playgroud)

- >每个字段(包括动态字段)都填充有效值.

我也试试这个:

foreach($form->all() as $item) {
       echo $item->getName();
        var_dump($item->getErrors());
}
Run Code Online (Sandbox Code Playgroud)

- >这些行不显示任何错误.但是,表格永远不会有效.

var_dump($form->isValid());
Run Code Online (Sandbox Code Playgroud)

- >返回false.所以表格无效.

最后,如果我删除整个动态部分,我的表单就可以了.我不明白什么是错的.表单没有显示错误,并且csrf标记似乎正确.我错过了什么 ?谢谢你的帮助.

Eth*_*nal 1

我知道这有点过时了,但在谷歌上的排名相当高。

getErrors() 方法仅返回表单的全局错误,而不返回底层字段的错误消息,在表单中使用嵌入表单时,您需要 getErrors(true) 或更复杂的方法。请参阅:https ://knpuniversity.com/blog/symfony-debugging-form-errors了解更多信息。