Created
January 31, 2019 09:43
-
-
Save ArtemioVegas/ea2b7ae4755e65f18326eefc88ac331d to your computer and use it in GitHub Desktop.
add symfony forms
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 | |
namespace App\Http\Action\Blog; | |
use App\ReadModel\PostReadRepository; | |
use Framework\Template\TemplateRenderer; | |
use Psr\Http\Message\ResponseInterface; | |
use Psr\Http\Message\ServerRequestInterface; | |
use Psr\Http\Server\RequestHandlerInterface; | |
use Zend\Diactoros\Response\EmptyResponse; | |
use Zend\Diactoros\Response\HtmlResponse; | |
use Symfony\Component\Validator\Constraints\Length; | |
use Symfony\Component\Validator\Constraints\NotBlank; | |
use Symfony\Component\Validator\Validation; | |
use Symfony\Bridge\Twig\Form\TwigRendererEngine; | |
use Symfony\Component\Form\Forms; | |
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; | |
use Symfony\Component\Form\Extension\Csrf\CsrfExtension; | |
use Symfony\Component\Security\Csrf\CsrfTokenManager; | |
use Symfony\Component\Translation\Translator; | |
use Symfony\Component\Translation\Loader\XliffFileLoader; | |
use Symfony\Bridge\Twig\Extension\TranslationExtension; | |
use Symfony\Bridge\Twig\Extension\FormExtension; | |
use Symfony\Bridge\Twig\Form\TwigRenderer; | |
class ShowAction implements RequestHandlerInterface | |
{ | |
private $posts; | |
private $template; | |
public function __construct(PostReadRepository $posts, TemplateRenderer $template) | |
{ | |
$this->posts = $posts; | |
$this->template = $template; | |
} | |
public function handle(ServerRequestInterface $request): ResponseInterface | |
{ | |
if (!$post = $this->posts->find($request->getAttribute('id'))) { | |
return new EmptyResponse(404); | |
} | |
// Set up the Validator component | |
$validator = Validation::createValidator(); | |
// Set up the Form component | |
$formFactory = Forms::createFormFactoryBuilder() | |
->addExtension(new ValidatorExtension($validator)) | |
->getFormFactory(); | |
// Create our first form! | |
$form = $formFactory->createBuilder() | |
->add('firstName', 'text', array( | |
'constraints' => array( | |
new NotBlank(), | |
new Length(array('min' => 4)), | |
), | |
)) | |
->add('lastName', 'text', array( | |
'constraints' => array( | |
new NotBlank(), | |
new Length(array('min' => 4)), | |
), | |
)) | |
->add('gender', 'choice', array( | |
'choices' => array('m' => 'Male', 'f' => 'Female'), | |
)) | |
->add('newsletter', 'checkbox', array( | |
'required' => false, | |
)) | |
->getForm(); | |
if (isset($_POST[$form->getName()])) { | |
$form->submit($_POST[$form->getName()]); | |
if ($form->isValid()) { | |
var_dump('VALID', $form->getData()); | |
die; | |
} | |
} | |
return new HtmlResponse($this->template->render('app/blog/show', [ | |
'post' => $post, | |
'form' => $form->createView(), | |
])); | |
} | |
} |
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 | |
namespace Infrastructure\Framework\Template\Twig; | |
use Framework\Template\Twig\Extension\RouteExtension; | |
use Psr\Container\ContainerInterface; | |
use Twig\Environment; | |
use Twig\Extension\DebugExtension; | |
use Twig\Loader\FilesystemLoader; | |
use Symfony\Component\Validator\Validation; | |
use Symfony\Bridge\Twig\Form\TwigRendererEngine; | |
use Symfony\Component\Form\Forms; | |
use Symfony\Component\Form\Extension\Validator\ValidatorExtension; | |
use Symfony\Component\Form\Extension\Csrf\CsrfExtension; | |
use Symfony\Component\Security\Csrf\CsrfTokenManager; | |
use Symfony\Component\Translation\Translator; | |
use Symfony\Component\Translation\Loader\XliffFileLoader; | |
use Symfony\Bridge\Twig\Extension\TranslationExtension; | |
use Symfony\Bridge\Twig\Extension\FormExtension; | |
use Symfony\Bridge\Twig\Form\TwigRenderer; | |
define('DEFAULT_FORM_THEME', 'form_div_layout.html.twig'); | |
//define('VENDOR_DIR', realpath(__DIR__ . '/../vendor')); | |
define('VENDOR_DIR', '/home/kotov/PhpstormProjects/php-demo-psr7-framework/vendor'); | |
define('VENDOR_FORM_DIR', VENDOR_DIR . '/symfony/form'); | |
define('VENDOR_VALIDATOR_DIR', VENDOR_DIR . '/symfony/validator'); | |
define('VENDOR_TWIG_BRIDGE_DIR', VENDOR_DIR . '/symfony/twig-bridge'); | |
define('VIEWS_DIR', realpath(__DIR__ . '/../views')); | |
$reflection = new \ReflectionClass(\Composer\Autoload\ClassLoader::class); | |
$vendorDir = dirname(dirname($reflection->getFileName())); | |
class TwigEnvironmentFactory | |
{ | |
public function __invoke(ContainerInterface $container) | |
{ | |
$debug = $container->get('config')['debug']; | |
$config = $container->get('config')['twig']; | |
$loader = new FilesystemLoader(); | |
$loader->addPath($config['template_dir']); | |
$loader->addPath(VENDOR_TWIG_BRIDGE_DIR . '/Resources/views/Form'); | |
$environment = new Environment($loader, [ | |
'cache' => $debug ? false : $config['cache_dir'], | |
'debug' => $debug, | |
'strict_variables' => $debug, | |
'auto_reload' => $debug, | |
]); | |
// Set up the Translation component | |
$translator = new Translator('en'); | |
$translator->addLoader('xlf', new XliffFileLoader()); | |
$translator->addResource('xlf', VENDOR_FORM_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators'); | |
$translator->addResource('xlf', VENDOR_VALIDATOR_DIR . '/Resources/translations/validators.en.xlf', 'en', 'validators'); | |
$formEngine = new TwigRendererEngine(array(DEFAULT_FORM_THEME)); | |
$formEngine->setEnvironment($environment); | |
$environment->addExtension(new TranslationExtension($translator)); | |
$environment->addExtension( | |
new FormExtension(new TwigRenderer($formEngine)) | |
); | |
if ($debug) { | |
$environment->addExtension(new DebugExtension()); | |
} | |
$environment->addExtension($container->get(RouteExtension::class)); | |
foreach ($config['extensions'] as $extension) { | |
$environment->addExtension($container->get($extension)); | |
} | |
return $environment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment