开发者

Unable to get a form to work properly in Symfony2 project

开发者 https://www.devze.com 2023-04-02 18:15 出处:网络
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 Conta

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->createFormBuil开发者_如何学编程der($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 }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消