title: Symfony 2.3/2.4 Standalone Form Component
body: <p>I am trying to use the Symfony 2.3 Form Standalone component to use outside of Symfony Framework.</p>
  
  <p>I tried the following url <a href="http://n.clavaud.free.fr/blog/index.php?article31/symfony2-standalone-form-component-tutorial" rel="nofollow">http://n.clavaud.free.fr/blog/index.php?article31/symfony2-standalone-form-component-tutorial</a></p>
  
  <p>in the above url code good but it is based on the old versions of symfony framework.</p>
  
  <p>I am getting problems while using Symfony 2.3</p>
  
  <p>I want the code same like above in the url but it should work on Symfony 2.3 or any other suggestions?</p>
  
  <p>I am using below code</p>
  
  <pre><code>&lt;?php
  
  use Symfony\Component\ClassLoader\UniversalClassLoader;
  
  use Symfony\Component\Form\Forms;
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\Form\FormFactory;
  use Symfony\Component\Form\FormRenderer;
  use Symfony\Component\Form\FormRendererInterface;
  use Symfony\Component\Form\Extension\Core\CoreExtension;
  use Symfony\Component\Form\Extension\Csrf\CsrfExtension;
  
  use Symfony\Component\EventDispatcher\EventDispatcher;
  
  use Symfony\Component\Templating\TemplateNameParser;
  use Symfony\Component\Templating\TemplateNameParserInterface;
  use Symfony\Component\Templating\TemplateReference;
  use Symfony\Component\Templating\Loader\FilesystemLoader;
  use Symfony\Component\Templating\PhpEngine;
  
  use Symfony\Component\Translation\MessageSelector;
  use Symfony\Component\Translation\Translator;
  use Symfony\Component\Translation\TranslatorInterface;
  use Symfony\Component\Validator\DefaultTranslator;
  
  use Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper;
  use Symfony\Bundle\FrameworkBundle\Templating\Helper\TranslatorHelper;
  
  // validation
  use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
  use Symfony\Component\Validator\Validator;
  use Symfony\Component\Validator\Mapping\ClassMetadata;
  use Symfony\Component\Validator\Mapping\ClassMetadataFactory;
  use Symfony\Component\Validator\Mapping\Loader\StaticMethodLoader;
  use Symfony\Component\Validator\ConstraintValidatorFactory;
  use Symfony\Component\Validator\Constraints as Constraints;
  use Symfony\Component\Validator\Validation;
  
  /**
   * Load and configure autoloader
   * @see http://symfony.com/doc/2.0/cookbook/tools/autoloader.html
   */
  require_once 'Symfony/Component/ClassLoader/UniversalClassLoader.php';
  
  $loader = new UniversalClassLoader();
  $loader-&gt;register();
  $loader-&gt;registerNamespace('Symfony', __DIR__.'/');
  
  /**
   * Parameters
   */
  $locale = null;
  
  /**
   * Entity
   */
  class Message
  {
  
      public $sender;
      public $recipient;
      public $message;
  
      // validation
      public static function loadValidatorMetadata(ClassMetadata $metadata)
      {
          $metadata-&gt;addPropertyConstraint('sender', new Constraints\NotBlank());
          $metadata-&gt;addPropertyConstraint('sender', new Constraints\Email());
          $metadata-&gt;addPropertyConstraint('recipient', new Constraints\NotBlank());
          $metadata-&gt;addPropertyConstraint('recipient', new Constraints\Email());
          $metadata-&gt;addPropertyConstraint('message', new Constraints\NotBlank());
          $metadata-&gt;addPropertyConstraint('message', new Constraints\MinLength(10));
      }
  
  }
  
  /**
   * Form Class
   * @see http://symfony.com/doc/2.0/book/forms.html#creating-form-classes
   */
  class MessageType extends AbstractType
  {
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder
          -&gt;add('sender', 'email')
          -&gt;add('recipient', 'email')
          -&gt;add('message', 'textarea');
      }
  
      public function getName()
      {
          return 'message';
      }
  
      public function getDefaultOptions(array $options)
      {
          return array(
              'data_class' =&gt; 'new\Message',
          );
      }
  
  }
  
  /**
   * Template name parser
   * @see Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTemplateNameParser
   *
   * Needed to load the templates used for rendering form items.
   */
  class StubTemplateNameParser implements TemplateNameParserInterface
  {
      private $root;
  
      private $rootTheme;
  
      public function __construct($root, $rootTheme)
      {
          $this-&gt;root = $root;
          $this-&gt;rootTheme = $rootTheme;
      }
  
      public function parse($name)
      {
  
          list($bundle, $controller, $template) = explode(':', $name);
  
          if ($template[0] == '_') {
              $path = $this-&gt;rootTheme.'/Custom/'.$template;
          } elseif ($bundle === 'TestBundle') {
              $path = $this-&gt;rootTheme.'/'.$controller.'/'.$template;
          } else {
              $path = $this-&gt;root.'/'.$controller.'/'.$template;
          }
  
          return new TemplateReference($path, 'php');
  
      }
  }
  
  /**
   * Create an entity
   */
  $message = new Message();
  $message-&gt;sender = 'mymail@example.com';
  
  /**
   * Build a form from a form factory
   */
  /* $form_factory = new FormFactory(array(
                                       new CoreExtension(),
                                       // validation
                                       new ValidatorExtension(
                                           new Validator(
                                               new ClassMetadataFactory(
                                                   new StaticMethodLoader()
                                               ),
                                               new ConstraintValidatorFactory(),
                                               new DefaultTranslator()
                                           )
                                       )
                                  ));*/
  $validator = Validation::createValidator();
  $form_factory = Forms::createFormFactoryBuilder()
                 -&gt;addExtension(new CoreExtension())
                 -&gt;addExtension(new ValidatorExtension($validator))
                 -&gt;getFormFactory();
  $form = $form_factory-&gt;create(new MessageType(), $message);
  
  /**
   * Create a PHP template engine
   */
  $root = realpath(__DIR__ . '/Symfony/Bundle/FrameworkBundle/Resources/views');
  $rootTheme = realpath(__DIR__ . '/Symfony/Bundle/FrameworkBundle/Resources');
  $templateNameParser = new StubTemplateNameParser($root, $rootTheme);
  $loader = new FilesystemLoader(array());
  $engine = new PhpEngine($templateNameParser, $loader);
  
  /**
   * This helper will help rendering form items
   */
  /*$form_helper = new FormHelper($engine, array(
                                              'FrameworkBundle:Form',
                                         ));*/
  $form_helper = new FormHelper(new FormRendererInterface());
  
  /**
   * Bind it to the engine
   */
  $engine-&gt;setHelpers(array(
                           $form_helper,
                           new TranslatorHelper(new Translator($locale, new MessageSelector())),
                      ));
  
  /**
   * Bind submitted data
   */
  $submitted = false;
  $valid = null;
  if (isset($_POST[$form-&gt;getName()])) {
      $form-&gt;bind($_POST[$form-&gt;getName()]);
      $submitted = true;
      // validation
      if ($valid = $form-&gt;isValid()) {
          // you may want to redirect at this state
          $data = $form-&gt;getData();
          echo 'validated success';
          var_dump($data);
      }
  }
  
  /**
   * Create the form view
   */
  $form_view = $form-&gt;createView();
  
  /**
   * Now, it's time to render HTML!
   */
  header('Content-type: text/html; charset=utf-8');
  ?&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;body&gt;
  &lt;form action="" method="post"
      &lt;?php print $form_helper-&gt;enctype($form_view) ?&gt;
        novalidate="novalidate"&gt;
      &lt;?php print $form_helper-&gt;widget($form_view) ?&gt;&lt;/div&gt;
      &lt;input type="submit" /&gt;
  &lt;/form&gt;
  
  &lt;?php if ($submitted &amp;&amp; $valid) : ?&gt;
      &lt;p&gt;&lt;strong&gt;Submitted form is valid.&lt;/strong&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;
  
  &lt;p&gt;&lt;em&gt;Message object:&lt;/em&gt;&lt;/p&gt;
  &lt;pre&gt;&lt;?php print print_r($message, true); ?&gt;&lt;/pre&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I am getting this error</p>
  
  <blockquote>
    <p>Fatal error: Cannot instantiate interface Symfony\Component\Form\FormRendererInterface in /var/www/new/test.php on line 193</p>
  </blockquote>