Search code examples
symfonysymfony-formssymfony-componentssymfony2

Using symfony 2.0.x form component standalone


How does one use the symfony-form component standalone for version 2.0.*? The examples in the documentation show how to create forms, but they use other parts of the symfony ecosystem, like controllers and actions that I currently do not know anything about, and I don't want to add those other components to my project.

I'm stuck using a system that has php 5.3.27, so I am unable to use a later version of symfony.


Solution

  • What I've done works so far, I just looked at the source code of the form component tests to figure out how forms were instantiated without the use of the symfony framework. Here is example code that worked for me:

    use Symfony\Component\Form\Extension\Core\CoreExtension;
    use Symfony\Component\Form\FormBuilder;
    use Symfony\Component\Form\FormFactory;
    use Symfony\Component\HttpFoundation\Request;
    
    $request = Request::createFromGlobals();
    
    $coreExt = new CoreExtension();    
    $formFactory = new FormFactory(array($coreExt));
    $formBuilder = $formFactory->createBuilder('form');
    
    $formBuilder->add('firstname', 'text');
    $form = $formBuilder->getForm();
    $form->bindRequest($request);
    $formView = $form->createView();