模态窗口中的树枝形式不提交数据 [英] twig form in modal window not submitting data

查看:31
本文介绍了模态窗口中的树枝形式不提交数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提交一个出现在模式窗口中的表单.我知道如果我将它作为单独的窗口运行,该表单可以工作,但是在模式中它似乎没有提交任何内容并且在 Symfony 中没有返回任何错误或任何内容.

这是我在树枝上的称呼:

 <button data-toggle="modal" href="{{ path('new_ingredient') }}" data-target="#ingredient_new">添加新成分</button><div class="modalfade" id="ingredient_new"><div class="modal-dialog"><div class="modal-content"><div class="modal-body">{{ 渲染(控制器('App\\Controller\\DishController:newIngredient'))}}

这是我的控制器:

/*** @Route("/ingredient/new", name="new_ingredient")*/公共函数 newIngredient(Request $request){$ingredient = new Ingredient();$form = $this->createForm(NewIngredientType::class, $ingredient);$form->handleRequest($request);if($form->isSubmitted() && $form->isValid()) {$ingredient = $form->getData();$em = $this->getDoctrine()->getManager();$em->persist($ingredient);$em->flush();返回 $this->redirectToRoute('dashboard');}return $this->render('ingredients/new.html.twig', array('形式' =>$form->createView()));}

我觉得这里的树枝上少了点什么.有什么明显的我遗漏了吗?就像我提到的,如果我单独调用它,表单本身就可以工作,但是提交后它在模态中什么也不做.

编辑这是表格本身;很简单,只有一个字段和一个提交

class NewIngredientType 扩展 AbstractType{公共函数 buildForm(FormBuilderInterface $builder, array $options){parent::buildForm($builder, $options);//TODO: 更改自动生成的存根$ingredient = new Ingredient();$builder->add('name', TextType::class, array('属性' =>array('class' => 'form-control'),))->add('save', SubmitType::class, array('标签' =>'节省','属性' =>数组('class' => 'btn btn-primary mt-3')))->getForm();}}

用于创建模态窗口的树枝是这样的:

{{ form_start(form) }}{{ form_widget(form) }}{{ form_end(form) }}

解决方案

好的,经过一番搜索并在上面@msg的帮助下,我想出了一个解决方案.

我需要为窗口中生成的表单显式定义操作,并且它起作用了.

这是我使用的变体

{{ form_start(form, {'action' : path('new_ingredient')}) }}{{ form_widget(form) }}{{ form_end(form) }}

I'm attempting to submit a form which appears in a modal window. I know that the form works if I run it as a separate window, however in the modal it appears to be submitting nothing and returning no errors or anything in Symfony.

Here is how I call it in the twig:

    <button data-toggle="modal" href="{{ path('new_ingredient') }}" data-target="#ingredient_new">Add a new ingredient</button>


<div class="modal fade" id="ingredient_new">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-body">
                {{ render(controller('App\\Controller\\DishController:newIngredient')) }}

            </div>
        </div>
    </div>

</div>

And here is my controller:

/**
 * @Route("/ingredient/new", name="new_ingredient")
 */
public function newIngredient(Request $request)
{

    $ingredient = new Ingredient();

    $form = $this->createForm(NewIngredientType::class, $ingredient);

    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()) {
        $ingredient = $form->getData();
        $em = $this->getDoctrine()->getManager();

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


        return $this->redirectToRoute('dashboard');

    }

    return $this->render('ingredients/new.html.twig', array(
            'form' => $form->createView())
    );
}

I feel like there's something missing in the twig here. Is there something obvious I'm missing? Like I mentioned, the form itself works if I call it separately, but after submitting it does nothing in the modal.

Edit Here's the form itself; it's quite simple, only one field and a submit

class NewIngredientType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options); // TODO: Change the autogenerated stub

        $ingredient = new Ingredient();

        $builder

            ->add('name', TextType::class, array(
                'attr' => array('class' => 'form-control'),

            ))


            ->add('save', SubmitType::class, array(
                'label' => 'Save',
                'attr' => array('class' => 'btn btn-primary mt-3')
            ))
            ->getForm();

    }

}

The twig that is used to create the modal window is this:

{{ form_start(form) }}
{{ form_widget(form) }}
{{ form_end(form) }}

解决方案

okay, after some searching and with the help of @msg above, I came up with a solution.

I needed to explicitly define the action for the generated form in the window, and it worked.

This is a variation on what I used

{{ form_start(form, {'action' : path('new_ingredient')}) }}
{{ form_widget(form) }}
{{ form_end(form) }}

这篇关于模态窗口中的树枝形式不提交数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆