无法在点击提交时提交表单

时间:2014-02-05 07:18:25

标签: php mysql forms symfony

您好我想在Symfony中单击提交按钮创建表单并将记录插入到mysql表中。 我是symfony的新手。我创建了表单,但它没有提交。这是我的代码

DefaultController.php

<?php

namespace Sym\FormBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sym\FormBundle\Entity\TblCust;
use Symfony\Component\HttpFoundation\Request;
use Sym\FormBundle\Form\TblCustType;


class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
        $tbl = new TblCust();


      //  $form = $this->createForm(new TblCustType(),$tbl);

        $form = $this->createFormBuilder($tbl)
                ->add('custName','text')
                ->add('custCity','text')
                ->add('custAddress','text')
                ->add('custPhno','text')
                ->add('save','submit')
                ->getForm();
        $form->handleRequest($request);
       if($form->isValid()){
            $em = $this->getDoctrine()->getManager();
            $em = persist('$tbl');
            $em->flush();
            return new response('New Customer Added..!');
       }
       $build['form']=$form->createView();
       return $this->render('FormBundle:Default:index.html.twig',array( 'form' => $form->createView(),));


    }
}

的routing.yml

form_homepage:
    pattern:  /Form
    defaults: { _controller: FormBundle:Default:index }

index.html.twig

{% block gender_widget %}
    {% spaceless %}
        {% if form %}
            <ul {{ block('widget_container_attributes') }}>
            {% for child in form %}
                <li>
                    {{ form_widget(child) }}
                    {{ form_label(child) }}
                </li>
            {% endfor %}
            </ul>
        {% else %}
            {# just let the choice widget render the select tag #}
            {{ block('choice_widget') }}
        {% endif %}
    {% endspaceless %}
{% endblock %}
{% block container %}

{% endblock%}

我使用doctrine:generate:bundle symfony推荐

创建了表单

TblCustType.php

<?php

namespace Sym\FormBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class TblCustType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('custName')
            ->add('custCity')
            ->add('custAddress')
            ->add('custPhno')
        ;
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Sym\FormBundle\Entity\TblCust'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'sym_formbundle_tblcust';
    }
}

我的输出如下图所示

Output

我的问题是,当我点击提交按钮时,它不会返回响应,我无法保存我的记录。请帮帮我。

2 个答案:

答案 0 :(得分:2)

我相信你需要把表格标签放在树枝上:

{{ form_start(form) }}

//all form widgets

//submit button

{{ form_rest(edit_form) }} <-- so it renders the hidden ones like the csrf token

</form>

答案 1 :(得分:1)

您需要使用form_startform_restform_end

{% block gender_widget %}
{% spaceless %}
    {% if form %}
    {{ form_start(form) }}
        <ul {{ block('widget_container_attributes') }}>
        {% for child in form %}
            <li>
                {{ form_widget(child) }}
                {{ form_label(child) }}
            </li>
        {% endfor %}
        </ul>
    {{ form_rest(form) }}
    {{ form_end(form) }}
    {% else %}
        {# just let the choice widget render the select tag #}
        {{ block('choice_widget') }}
    {% endif %}
{% endspaceless %}
{% endblock %}