Created
February 4, 2015 15:43
-
-
Save jakzal/483dc80b42200199836a to your computer and use it in GitHub Desktop.
form.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"symfony/form": "2.6.2", | |
"symfony/http-foundation": "2.6.2", | |
"symfony/twig-bridge": "2.3.*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Bridge\Twig\Extension\FormExtension; | |
use Symfony\Bridge\Twig\Form\TwigRenderer; | |
use Symfony\Bridge\Twig\Form\TwigRendererEngine; | |
use Symfony\Component\Form\Extension\Core\CoreExtension; | |
use Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension; | |
use Symfony\Component\Form\Forms; | |
use Symfony\Component\HttpFoundation\Request; | |
require_once __DIR__.'/vendor/autoload.php'; | |
function get_form_factory() | |
{ | |
$formFactory = Forms::createFormFactoryBuilder() | |
->addExtension(new HttpFoundationExtension()) | |
->addExtension(new CoreExtension()) | |
->getFormFactory(); | |
return $formFactory; | |
} | |
function get_twig() | |
{ | |
$twig = new Twig_Environment(new Twig_Loader_Array(array( | |
'form.html.twig' => '{{ form(form) }}', | |
'form_div_layout.html.twig' => file_get_contents('./vendor/symfony/twig-bridge/Symfony/Bridge/Twig/Resources/views/Form/form_div_layout.html.twig') | |
))); | |
$formEngine = new TwigRendererEngine(array('form_div_layout.html.twig')); | |
$formEngine->setEnvironment($twig); | |
$twig->addExtension( | |
new FormExtension(new TwigRenderer($formEngine)) | |
); | |
$twig->addFilter(new Twig_SimpleFilter('trans', function ($string) { | |
return $string; | |
})); | |
return $twig; | |
} | |
function handle_request(Request $request) | |
{ | |
$twig = get_twig(); | |
$form = get_form_factory()->createBuilder('form') | |
->add('name', 'text') | |
->add('send', 'submit') | |
->getForm(); | |
if ($request->isMethod('POST')) { | |
var_dump($request->request); | |
var_dump($request->get('name')); | |
var_dump($request->request->get('name')); | |
} | |
$form->handleRequest($request); | |
if($form->isValid()) { | |
die('valid!'); | |
} | |
echo $twig->render('form.html.twig', array('form' => $form->createView())); | |
} | |
handle_request(Request::createFromGlobals()); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment