【问题标题】:Symfony - adding record to dbSymfony - 将记录添加到数据库
【发布时间】:2018-09-21 09:26:26
【问题描述】:

我使用具有将新数据添加到数据库的操作的表单编写了代码。似乎一切都以正确的方式连接,但“添加”按钮没有响应,到目前为止我没有收到任何错误。

我的控制器

 /**
 * @Route("/theater-add", name="theater_add")
 * @param Request $request
 * @return \Symfony\Component\HttpFoundation\Response
 * @throws \Exception
 */
public function theaterAddAction(Request $request)
{

    $form = $this->createFormBuilder()
        ->add('name', TextType::class, array('label' => 'Theater Name', 'attr' => ['class'=>'form-control']))
        ->add('town', TextType::class, array('label' => 'Town', 'attr' => ['class'=>'form-control']))
        ->add('address', TextType::class, array('label' => 'Address', 'attr' => ['class'=>'form-control']))
        ->add('phone', TextType::class, array('label' => 'Phone', 'attr' => ['class'=>'form-control']))
        ->add('capacity', TextType::class, array('label' => 'Capacity', 'attr' => ['class'=>'form-control']))
        ->add('seats', TextType::class, array('label' => 'Seats', 'attr' => ['class'=>'form-control']))
        ->add('save', SubmitType::class, array('label' => 'Add', 'attr' => ['class' => 'btn btn-primary pull-right action-save']))
        ->getForm();


   $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $data = $form->getData();
        $name = $data['name'];
        $town = $data['town'];
        $address = $data['address'];
        $phone = $data['phone'];
        $capacity = $data['capacity'];
        $seats = $data['seats'];

        $this->container->get('theater')->addNewTheater($name, $town, $address, $phone, $capacity, $seats);
    }

    $build['form'] = $form->createView();
    return $this->render('@AdminTemplates/pages/theater-add.html.twig', $build);
}

服务

public function addNewTheater($name, $town, $address, $phone, $capacity, $seats)
{
    $newTheater = new Theater();
    $newTheater -> setName($name);
    $newTheater -> setTown($town);
    $newTheater -> setAddress($address);
    $newTheater -> setPhone($phone);
    $newTheater -> setCapacity($capacity);
    $newTheater -> setSeats($seats);

    $this->em->persist($newTheater);
    $this->em->flush();

    return $newTheater;
}

我的树枝视图

{{ form_start(form, {'action': path('theater_add'), 'method': 'GET'}) }}
                <form>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group">
                                {{ form_row(form.name) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                {{ form_row(form.town) }}
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group">
                                {{ form_row(form.address) }}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-4">
                            {{ form_row(form.phone) }}
                        </div>
                        <div class="col-md-4">
                            {{ form_row(form.capacity) }}
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                {{ form_row(form.seats) }}
                            </div>
                        </div>
                    </div>
                </form>
                {{ form_end(form) }}

我需要某种类型的指导,因为我在网上找不到任何解决方案。

【问题讨论】:

    标签: database forms symfony add crud


    【解决方案1】:

    您的树枝模板中有错误:

    {{ form_start(form, {'action': path('theater_add'), 'method': 'GET'}) }}
    <form>
    ...
    </form>
    {{ form_end(form) }}
    

    {{ form_start(form) }} 将自己呈现为一个表单元素(在 HTML 模板中),{{ form_end(form) }} 呈现一个 &lt;/form&gt; 结束标记,因此基本上您创建以下 HTML:

    当您单击提交按钮时,内部表单被提交,它不处理控制器中的 $request 对象,因此 ($form-&gt;isSubmitted() &amp;&amp; $form-&gt;isValid()) 永远不会解析为 true 和以下 if (添加真正发生的地方)永远不会被执行。

    总结一下: 只需删除模板中的 form_start 之后和 form_end 之前。

    【讨论】:

    • 我刚刚看到了。非常感谢。
    【解决方案2】:

    这是一个简单的错误..

    我关闭了两次表单标签,所以添加按钮不在表单中..

    </form>
    {{ form_end(form) }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-23
      • 2011-12-17
      • 1970-01-01
      • 2021-02-18
      • 2017-11-20
      • 1970-01-01
      相关资源
      最近更新 更多