title: Symfony2: how to call templating service in a formHandler?
body: <p>I'm trying to do a contact form with Symfony 2 (version 2.4.1). My form is without an entity because it doesn't have to store informations in a database (just to send mails). I have a problem to call the templating render (or maybe renderView?) method for the mail template in the form handler class : </p>
  
  <pre><code>&lt;?php
  // src/Open/OpcBundle/Form/Handler/Handler.php
  
  namespace Open\OpcBundle\Form\Handler;
  
  use Symfony\Component\Form\Form;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\Templating\EngineInterface;
  
  class ContactHandler {
      protected $request;
      protected $form;
      protected $mailer;
      protected $templating;
  
  public function __construct(Form $form, Request $request, $mailer, EngineInterface $templating) {
    $this-&gt;form = $form;
    $this-&gt;request = $request;
    $this-&gt;mailer = $mailer;
    $this-&gt;templating = $templating;
  }
  
  public function process() {
     if ('POST' == $this-&gt;request-&gt;getMethod()) {
         $this-&gt;form-&gt;handleRequest($this-&gt;request);
         $data = $this-&gt;form-&gt;getData();
         $this-&gt;onSuccess($data);
         return true;
     }
  
     return false;
  }
  
  protected function onSuccess($data) {
      $message = \Swift_Message::newInstance()
                 -&gt;setContentType('text/html')
                 -&gt;setSubject($data['sujet'])
                 -&gt;setFrom($data['courriel'])
                 -&gt;setTo('me@gmail.com')
                  -&gt;setBody($this-&gt;templating-&gt;renderView('OpcOpenBundle:Opc:Mails/contact.html.twig',
                                             array('ip' =&gt; $request-&gt;getClientIp(),
                                             'nom' =&gt; $data['nom'],
                                             'msg' =&gt; $data['msg'])
                                            )
                           );
  
     $this-&gt;get('mailer')-&gt;send($message);
  
     $request-&gt;getSession()-&gt;getFlash()-&gt;add('success', 'Your email has been sent! Thanks!');
  
     return $this-&gt;redirect($this-&gt;generateUrl('contact'));
  }
  }
  </code></pre>
  
  <p>This produce the following error: </p>
  
  <pre><code>ContextErrorException: Catchable Fatal Error: Argument 4 passed to Open\OpcBundle\Form\Handler\ContactHandler::__construct() must be an instance of Symfony\Component\Templating\EngineInterface, none given, called in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Controller\OpcController.php on line 135 and defined in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 18
  </code></pre>
  
  <p>So I tried to put the templating argument in the FormHandler call in the controller:</p>
  
  <pre><code>&lt;?php
  // src/Open/OpcBundle/Controller/OpcController.php
  
  namespace Open\OpcBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Open\OpcBundle\Form\Type\ContactType;
  use Open\OpcBundle\Form\Handler\ContactHandler;
  
  class OpcController extends Controller {
  
     public function contactAction(Request $request) {      
         $form = $this-&gt;get('form.factory')-&gt;create(new ContactType());
  
         $request = $this-&gt;get('request');
  
         $formHandler = new ContactHandler($form, $request, $this-&gt;get('mailer'), $this-&gt;get('templating'));
  
         $process = $formHandler-&gt;process();
  
        if ($process) {
           $this-&gt;get('session')-&gt;setFlash('notice', 'Merci de nous avoir contacté, nous répondrons à vos questions dans les plus brefs délais.');
    }
  
    return $this-&gt;render("OpenOpcBundle:Opc:contact.html.twig",
          array("form" =&gt; $form-&gt;createView(),
                  "hasError" =&gt; $request-&gt;getMethod() == 'POST' &amp;&amp; !$form-&gt;isValid()
          )
    );      
  </code></pre>
  
  <p>} // -- contactAction()<br>
  } // -- class</p>
  
  <p>But this produce another error, no more $request: </p>
  
  <pre><code>ContextErrorException: Notice: Undefined variable: request in C:\Program Files\wamp\www\sf2\src\Open\OpcBundle\Form\Handler\ContactHandler.php line 43
  </code></pre>
  
  <p>Any idea ? Maybe I should use another method (more simple, without handler) to do my contact form with Symfony 2?</p>