无法让表单在 Symfony2 项目中正常工作
我正在为我正在从事的项目建立(并努力)联系表格。我一直在关注这个“教程”这里,首先我改变了一些内容适合我的项目的教程。因此,在我的 Contactform.php 文件(教程中的 Task.php 文件)中,我的代码如下所示:
namespace Shout\MainBundle\Entity;
class Contactform { 受保护的$你的名字;
protected $youremail;
protected $yourphone;
protected $yourenquiry;
protected $newsletter;
protected $events;
public function getYourname()
{
return $this->yourname;
}
public function setYourname($yourname)
{
$this->yourname = $yourname;
}
public function getYouremail()
{
return $this->youremail;
}
public function setYouremail($youremail)
{
$this->youremail = $youremail;
}
public function getYourphone()
{
return $this->yourphone;
}
public function setYourphone($yourphone)
{
$this->yourphone = $yourphone;
}
public function getYourenquiry()
{
return $this->yourenquiry;
}
public function setYourenquiry($yourenquiry)
{
$this->YourEnquiry = $yourenquiry;
}
public function getNewsletter()
{
return $this->newsletter;
}
public function setNewsletter($newsletter)
{
$this->newsletter = $newsletter;
}
public function getEvents()
{
return $this->events;
}
public function setEvents($events)
{
$this->events = $events;
}
在 DefaultClasses.php 文件(我在其中处理联系表单呈现)中,我有以下代码:
public function contactAction(Request $request)
{
$contactform = new Contactform();
$form = $this->createFormBuilder($contactform)
->add('yourname', 'text')
->add('youremail', 'text')
->add('yourphone', 'text')
->add('yourenquiry', 'text')
->add('newsletter', 'text')
->add('events', 'text')
->getForm();
return $this->render('ShoutMainBundle:Default:contact.html.twig', array('form' => $form->createView()));
}
显然这与教程不同,因为我不想用数据预先填充它。
然后,在 contact.php.twig 文件中,我有以下代码调用联系表单:
<form action="{{ path('form_contact') }}" method="post" {{ form_enctype(cform) }}>
{{ form_widget(cform) }}
<input type="submit" />
</form>
但是,我不断收到以下错误:
在第 62 行的“ShoutMainBundle:Default:contact.html.twig”中渲染模板期间抛出异常(“Route“form_contact”不存在。”)。
我一直参考本教程的原因是,我认为我在编辑原始代码时犯了一个错误,因此我将教程中的所有代码复制并粘贴到我的项目中。但我仍然收到此错误。
我花了周五的最后一部分时间和今天早上的大部分时间来处理这个问题,但我仍然遇到了困难。我是否错过了什么或者我走错了路?
编辑: 这是路由文件:
ShoutMainBundle_mainpage:
pattern: /mainpage/{slug}
defaults: { _controller: ShoutMainBundle:Default:mainpage }
ShoutMainBundle_subpage:
pattern: /mainpage/{page}/{slug}
defaults: { _controller: ShoutMainBundle:Default:subpage }
ShoutMainBundle_news:
pattern: /news
defaults: { _controller: ShoutMainBundle:News:index }
ShoutMainBundle_newsarticle:
pattern: /news/article/{article}
defaults: { _controller: ShoutMainBundle:News:article }
ShoutMainBundle_event:
pattern: /event
defaults: { _controller: ShoutMainBundle:Event:index }
ShoutMainBundle_eventselected:
pattern: /event/{id}/{event}
defaults: { _controller: ShoutMainBundle:Event:selected }
ShoutMainBundle_blog:
pattern: /blog
defaults: { _controller: ShoutMainBundle:Blog:index }
ShoutMainBundle_blogarticle:
pattern: /blog/article/{article}
defaults: { _controller: ShoutMainBundle:Blog:article }
ShoutMainBundle_blogcategorieshome:
pattern: /blog/categories/
defaults: { _controller: ShoutMainBundle:Blog:categoryhome }
ShoutMainBundle_blogcategoriesselected:
pattern: /blog/category/{category}
defaults: { _controller: ShoutMainBundle:Blog:categoryselected }
ShoutMainBundle_blogarchive:
pattern: /blog/archive/{category}
defaults: { _controller: ShoutMainBundle:Blog:archive }
ShoutMainBundle_contact:
pattern: /contact-us
defaults: { _controller: ShoutMainBundle:Default:contact }
I am building (well trying to) a contact form for a project I'm working on. I have been following this "tutorial" here and at first I changed bits of the tutorial to fit my project. So, in my Contactform.php file (which is what the Task.php file is in the tutorial) my code looks like this:
namespace Shout\MainBundle\Entity;
class Contactform
{
protected $yourname;
protected $youremail;
protected $yourphone;
protected $yourenquiry;
protected $newsletter;
protected $events;
public function getYourname()
{
return $this->yourname;
}
public function setYourname($yourname)
{
$this->yourname = $yourname;
}
public function getYouremail()
{
return $this->youremail;
}
public function setYouremail($youremail)
{
$this->youremail = $youremail;
}
public function getYourphone()
{
return $this->yourphone;
}
public function setYourphone($yourphone)
{
$this->yourphone = $yourphone;
}
public function getYourenquiry()
{
return $this->yourenquiry;
}
public function setYourenquiry($yourenquiry)
{
$this->YourEnquiry = $yourenquiry;
}
public function getNewsletter()
{
return $this->newsletter;
}
public function setNewsletter($newsletter)
{
$this->newsletter = $newsletter;
}
public function getEvents()
{
return $this->events;
}
public function setEvents($events)
{
$this->events = $events;
}
In the DefaultClasses.php file (where I'm handling the contact form rendering) I have the following code:
public function contactAction(Request $request)
{
$contactform = new Contactform();
$form = $this->createFormBuilder($contactform)
->add('yourname', 'text')
->add('youremail', 'text')
->add('yourphone', 'text')
->add('yourenquiry', 'text')
->add('newsletter', 'text')
->add('events', 'text')
->getForm();
return $this->render('ShoutMainBundle:Default:contact.html.twig', array('form' => $form->createView()));
}
Obviously this is different to the tutorial as I don't want to pre-populate it with data.
Then, in the contact.php.twig file, I have the following code calling the contact form:
<form action="{{ path('form_contact') }}" method="post" {{ form_enctype(cform) }}>
{{ form_widget(cform) }}
<input type="submit" />
</form>
However, I keep getting the following error:
An exception has been thrown during the rendering of a template ("Route "form_contact" does not exist.") in "ShoutMainBundle:Default:contact.html.twig" at line 62.
The reason I have been referring to the tutorial is that, thinking I have made a mistake in editing the original code, I copy and pasted all of the code that's on the tutorial in to my project. But I still receive this error.
I spent the last part of Friday on this and the best part of this morning on it but I'm still hitting this brick wall. Is there something I've missed or am I going the wrong way about it?
Edit:
Here is the routing file:
ShoutMainBundle_mainpage:
pattern: /mainpage/{slug}
defaults: { _controller: ShoutMainBundle:Default:mainpage }
ShoutMainBundle_subpage:
pattern: /mainpage/{page}/{slug}
defaults: { _controller: ShoutMainBundle:Default:subpage }
ShoutMainBundle_news:
pattern: /news
defaults: { _controller: ShoutMainBundle:News:index }
ShoutMainBundle_newsarticle:
pattern: /news/article/{article}
defaults: { _controller: ShoutMainBundle:News:article }
ShoutMainBundle_event:
pattern: /event
defaults: { _controller: ShoutMainBundle:Event:index }
ShoutMainBundle_eventselected:
pattern: /event/{id}/{event}
defaults: { _controller: ShoutMainBundle:Event:selected }
ShoutMainBundle_blog:
pattern: /blog
defaults: { _controller: ShoutMainBundle:Blog:index }
ShoutMainBundle_blogarticle:
pattern: /blog/article/{article}
defaults: { _controller: ShoutMainBundle:Blog:article }
ShoutMainBundle_blogcategorieshome:
pattern: /blog/categories/
defaults: { _controller: ShoutMainBundle:Blog:categoryhome }
ShoutMainBundle_blogcategoriesselected:
pattern: /blog/category/{category}
defaults: { _controller: ShoutMainBundle:Blog:categoryselected }
ShoutMainBundle_blogarchive:
pattern: /blog/archive/{category}
defaults: { _controller: ShoutMainBundle:Blog:archive }
ShoutMainBundle_contact:
pattern: /contact-us
defaults: { _controller: ShoutMainBundle:Default:contact }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于“操作”部分试图找到不存在的页面。我删除了它并且它起作用了。现在将创建一个页面供其参考。
The issue was that the "action" part was trying to find a page that wasn't there. I removed that and it worked. Will now create a page for it to reference.