【问题标题】:Rendered form is not posting data呈现的表单未发布数据
【发布时间】:2017-08-29 11:25:05
【问题描述】:

我在我的 symfony 网站中使用了一个单页模板,它有一个联系表格部分。

我已将我的联系操作(appbundle:contact:new)呈现到我的主页。ContactController 是由学说 CRUD 自动生成的。

它完美地显示了表格。但它不执行任何操作。我错过了什么吗?

我的看法

{% block header %}{% include'header.html.twig' %}{% endblock %}
{% block nav %}{% include'nav.html.twig' %}{% endblock %}
{% block mainHeader %}{% include'mainHeader.html.twig' %}{% endblock %}
{% block body %} {% endblock %}
{% block projetCategories %}{% include'projetCategories.html.twig' %}{% endblock %}
{% block projets %}{% include'projets.html.twig' %}{% endblock %}
{#{% block contact %}{{ render(controller('BlogBundle:Contact:new')) }}{% endblock %}#}
{% block contact %}{% include':contact:new.html.twig' %}{% endblock %}
{% block footer %}{% include'footer.html.twig' %}{% endblock %}
{% block javascripts %}{% include'javascript.html.twig' %}{% endblock %}

我的控制器

class ContactController extends Controller
{
public function newAction(Request $request)
{
    $contact = new Contact();
    $form = $this->createForm('BlogBundle\Form\ContactType', $contact);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        //$contactData = [$form->getValue('nom'), $form->getValue('prenom'),$form->getValue('email'), $form->getValue('sujet'), $form->getValue('message')];
        $em = $this->getDoctrine()->getManager();
        $em->persist($contact);
        $em->flush();

        return $this->redirectToRoute('blog_homepage', array('id' => $contact->getId()));
    }

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

我的模型

class Contact {
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
public $id;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $nom;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $prenom;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $email;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $sujet;

/**
 * @ORM\Column(type="string", nullable=true)
 */
private $message;

我的 new.html.twig

<div class="col-lg-6 text-center">
                {{ form_start(contactForm) }}
                {{ form_widget(contactForm) }}
                <input type="submit" value="Envoyer" />
                {{ form_end(contactForm) }}

我的表单

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('nom', TextType::class, array('label'=>false, 'attr'=>array(
            'class'=>'form-control',
            'placeholder'=>'Nom*',
            'style'=>"margin-bottom:5px;"
            )));
        $builder->add('prenom', TextType::class, array('label'=>false, 'attr'=>array(
            'class'=>'form-control',
            'placeholder'=>'Prénom*',
            'style'=>"margin-bottom:5px;"
            )));
        $builder->add('email', EmailType::class, array('label'=>false, 'attr'=>array(
            'class'=>'form-control',
            'placeholder'=>'Email*',
            'style'=>"margin-bottom:5px;"
            )));
        $builder->add('sujet', ChoiceType::class, array( 'label'=>false,
            'choices' => array(
                'Quel sujet?'=> false, 
                '1'=>'1', 
                '2'=>'2', 
                '3'=>'3', 
            ),
            'choices_as_values' => true,
            'attr'=>array('class'=>'form-control','style'=>"margin-bottom:5px;"),
            'choice_attr' => function($val, $key, $index) {
                // adds a class like attending_yes, attending_no, etc
                return ['class' => strtolower($key)];
            },
        ));
        $builder->add('message', TextareaType::class, array('label'=>false, 'attr'=>array(
            'class'=>'form-control',
            'placeholder'=>'Message*',
            'style'=>"margin-bottom:5px;",
            'cols'=>50,
            'rows'=>10
        )));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'BlogBundle\Entity\Contact'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'blogbundle_contact';
    }

源码修改后的错误提示

Variable "contactForm" does not exist.

【问题讨论】:

  • 您渲染模板,在其中再次渲染模板并再次包含它... WTF ?
  • @t-n-y : 块被contactController的新动作覆盖!哇!清理一下您的规格?
  • 所以也许你没有在你的帖子中很好地指定它,但我在这里看到的是你渲染contact/new.html.twig 并且在那个文件中你调用同一个控制器的渲染......不要'在这里看不到任何替代品
  • 好吧,我想我明白了,你给我们看的不是contact/new.html.twig
  • @t-n-y 我尝试了多种方式:我已经尝试将 contact/new 渲染为 contact/new.html.twig。并包括 new.html.twigbase.html.twig。不幸的是,我遇到了这个错误变量“contactForm”不存在。

标签: php symfony model-view-controller doctrine


【解决方案1】:

是的,看起来您创建了一个new Contact(),但实际上您并没有在其中设置任何值!

试试这样的!

if ($form->isSubmitted() && $form->isValid()) {
    $dataArray = $form->getValuesOrWhateverMethod();
    $contact->setWhatever($dataArray['whatever']);
    // etc
    $em = $this->getDoctrine()->getManager();
    $em->persist($contact);
    $em->flush();
}

另外,你最好像这样拆分 if 语句:

if ($form->isSubmitted()) {
    if ($form->isValid()) {
        // populate entity and persist
    } else {
        // populate form with whatever WAS valid
        // send form not valid message to view 
    }
} 

【讨论】:

  • 抱歉,没看清楚!验证此路由“site_domain/contact/new”上的表单时,相同的方法操作起作用。例外地,它在我的主页上不起作用。
  • 我已使用此{% block contact %}{{ render(controller('BlogBundle:Contact:new')) }}{% endblock %} 将表单呈现到我的主页
【解决方案2】:
  1. 你能告诉我们ContactType的代码吗?
  2. 我建议您可以再次访问https://symfony.com/doc/current/forms.html#creating-form-classes,因为它显示的示例与您想要实现的目标相似。
  3. 使用 symfony 的 dump() 函数进行调试也应该非常方便。

【讨论】:

  • 是的,它是在我的包中创建的。我立即在我的问题中添加了这个
  • 添加了ContactType
猜你喜欢
  • 2013-12-03
  • 1970-01-01
  • 2014-09-15
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
相关资源
最近更新 更多