Symfony表单提交不起作用

时间:2017-06-13 18:35:54

标签: php symfony symfony-forms

我最近决定冒险尝试学习Symfony,就像我学习平面PHP一样。

我一直在关注如何通过Symfony 3中的表单向数据库添加数据的各种教程。我有一个名为CRM_PERSON的MySQL表,我正在尝试提交一个人的详细信息(一个人被称为“领导“在这个项目中”。出于某种原因,当我按下表单上的提交按钮时,我只是被发送到谷歌浏览器中的标准错误页面,说“无法访问此站点。

我已经尝试完全从三个不同的教程中跟踪代码和Stack Overflow上的各种问题,但我似乎仍然无法使其工作。我相信我必须在这里做些蠢事!

这是我的代码:

LeadController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Form\CrmPersonType;
use AppBundle\Entity\CrmPerson;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Doctrine\ORM\EntityManagerInterface;




class LeadController extends Controller
{
    /**
     * @Route("/user/lead", name="lead")
     */
    public function indexAction(Request $request)
    {

        $leads = $this->getDoctrine()->getRepository('AppBundle:CrmPerson')->findAll();

        dump($leads);

        return $this->render(':lead:lead.html.twig', [
        'leads' => $leads
        ]);
     }


    /**
     * @Route("/user/lead_add", name="lead_add")
     */
    public function leadAdd(Request $request)   
    {
        $form = $this->createForm(CrmPersonType::class);

        $form->handleRequest($request);

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

            $em = $this->getDoctrine()->getManager();

            $crmperson = $form->getData();

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

            return $this->redirectToRoute('lead_add');
        }

        return $this->render(':lead:lead.add.html.twig', [
        'leadForm' => $form->createView()
            ]);
    }



}

CrmPersonType.php

<?php

// src/AppBundle/Form/CrmPersonType.php
namespace AppBundle\Form;

use AppBundle\Entity\CrmPerson;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CrmPersonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('title', TextType::class)
            ->add('firstName', TextType::class) 
            ->add('middleNames', TextType::class) 
            ->add('surname', TextType::class)
            ->add('submit', SubmitType::class, [
                'label' => 'Save',
                'attr'  => [
                    'class' => 'btn btn-success'
                ]
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => CrmPerson::class,
        ));
    }
}

lead.add.html.twig

{# app/Resources/views/lead.html.twig #}

{% extends 'base.html.twig' %}

{% block body %}

<html>
    <body>
        <div class="container-fluid">
        <div class="panel panel-default">
        <div class="panel-body">
            <h1>Add Lead</h1>

            {{ form_start(leadForm) }}
            {{ form_widget(leadForm) }}
            {{ form_end(leadForm) }}

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



    </body>
</html>




{% endblock %}

我真的很感激正确方向的一些指示!

1 个答案:

答案 0 :(得分:1)

创建表单时应该提供一个新的CrmPerson

$crmPerson = new CrmPerson();
$form = $this->createForm(CrmPersonType::class, $crmPerson);