Dimitri
Dimitri

Reputation: 959

Symfony2 and embedded controller

I have a problem with my form service... In my footer (in a layout), I have a form but it not work and I dont know why. Do you how I can call the "createForm" method because the error returned by Symfony2.5 is :

Attempted to call method "createForm" on class "Dim\WebsiteBundle\Service\QuickContact" in .../Symfony/src/Dim/WebsiteBundle/Service/QuickContact.php line 47.

My service (namespace Dim\WebsiteBundle\Service) :

class QuickContact
{

  private $request;
  private $formFactory;

  public function setRequest(RequestStack $request_stack)
  {
    return $this->request = $request_stack->getCurrentRequest();
  }

  public function setFormFactory($formFactory)
  {   
    return $this->formFactory = $formFactory;
  }

  public function indexAction()
  { 
    $Contact = new Contact();

    # Create the form with the contact entity constraints
    $form = $this->formFactory->create(new ContactType(), $Contact);

    if($this->request->isMethod('POST')) 
    {
      $form->bind($this->request);

      if($form->isValid()) 
      {
       echo 'formulaire valide';
      }
    }

    $this->request->getSession()->set('form', $form);
  }

}

My service.yml :

services:
     dim_website.quickcontact:
         class: Dim\WebsiteBundle\Service\QuickContact
         calls:
            - [setRequest, [@request_stack], setFormFactory, [@form.factory]]

In my layout, I call the index method :

{{ quickcontact.indexAction() }}

For your information, my ContactType and his entity work fine.

Thanks you all for your help. Best regards,

Upvotes: 0

Views: 262

Answers (1)

Igor Pantović
Igor Pantović

Reputation: 9246

I'm not sure why you're doing things as you are, but to answer your question: you are calling method createForm on your QuickContact class which obviously doesn't have createForm method defined.

You should be calling $this->formFactory->create(new ContactType(), $Contact) instead in your case.

Upvotes: 1

Related Questions