0

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.

KyoreG
  • 191
  • 1
  • 7
  • The controllers they show are examples. You can easily adapt it to your own needs by simply making your own implementation that follows the same setup for the form. – Seer Jul 07 '14 at 19:46
  • Yeah, but that means I have to know how the controllers setup the form components, it seems like a pretty involved process that involves knowing how some of the symfony components work together, and there doesn't seem to be very good documentation on this. Regardless, I've looked through the source code and [code](https://github.com/symfony/symfony/blob/v2.0.25/tests/Symfony/Tests/Component/Form/FormFactoryTest.php) that tests the form component was a good starting point to see how things were instantiated. – KyoreG Jul 09 '14 at 16:20
  • You should look at the [correct documentation](http://symfony.com/doc/current/components/form/introduction.html), it'd probably help a lot! If you want to look at documentation for Symfony components by themselves then always look at the component documentation, not framework documentation. I know you need an earlier version of the docs, but just try those docs anyway, it's not going to be hugely different compared to the earliest version of those docs. – Seer Jul 09 '14 at 18:58
  • I checked out the 2.1 docs, there were a few helper functions that facilitated the creation of forms that were introduced in 2.1, but reading the source and re-implementing wasn't too hard. I'll post my solution as an answer. – KyoreG Jul 11 '14 at 20:38

2 Answers2

1

You are looking at the wrong documentation. If you want to use symfony2 forms as a stand alone component, you should look at the component documentation. http://symfony.com/doc/current/components/form/introduction.html

Use composer to determine the component version that matches your php version

sydney
  • 51
  • 4
  • The documentation you linked does not provide a section on 2.0.*, the earliest version they give documentation for is 2.1. On [packagist](https://packagist.org/packages/symfony/form) there is a form component for 2.0.*. They link to the form component github project page on the page that I provided but the github page links back to the referrer. – KyoreG Jul 09 '14 at 16:13
  • As Seer said, the main forms documentation should provide a proper guideline on how to use the symfony2 forms component as standalone. If you do get errors however, you can have a look at the upgrade files on the main symfony2 project https://github.com/symfony/symfony/blob/master/UPGRADE-2.1.md – sydney Jul 10 '14 at 04:19
1

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();
KyoreG
  • 191
  • 1
  • 7