gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:44:47 25 4
gpt4 key购买 nike

我在 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';
}
}

所以,这两个 addEventListener 工作得很好。第一次显示我的表单时,没有按预期加载 formModifier 中的字段。我的 Controller 类是以下一个:

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(),
);
}

由于我的一个动态字段不能为空,所以第一次提交表单时,它不能有效。因此,第二次加载 FormationType。这意味着,如果字段“type”被填充,我的 formModifier() 函数可以加载动态字段 (formationList)。在那之前,一切都很好,我得到了我的新领域。

但是,在表单上第二次“提交”之后……没有任何反应。页面刚刚重新加载,没有显示任何错误。

我用

检查了表单内容
var_dump($request->request->get('example_corebundle_formationtype'));

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

我也试试这个:

foreach($form->all() as $item) {
echo $item->getName();
var_dump($item->getErrors());
}

-> 这些行没有显示任何错误。但是,该表格永远无效。

var_dump($form->isValid());

-> 它返回 false。所以表格无效。

最后,如果我删除整个动态部分,我的表单就可以工作了。我不明白怎么了。表单没有显示任何错误,csrf token 似乎是正确的。我错过了什么 ?感谢您的帮助。

最佳答案

我知道这有点过时,但在 Google 上的排名很高。

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

关于php - Symfony2 无效的动态表单,没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19230532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com