Skip to content

Instantly share code, notes, and snippets.

@ahukkanen
Last active May 13, 2021 06:33
Show Gist options
  • Save ahukkanen/bd9c4c337492677d9327 to your computer and use it in GitHub Desktop.
Save ahukkanen/bd9c4c337492677d9327 to your computer and use it in GitHub Desktop.
Example of using Symfony forms without symfony/framework-bundle
<?php
use Mainio\Symfony\FormsIntegration\TemplateNameParser;
use Mainio\Symfony\FormsIntegration\Templating\Helper\FormHelper;
use Mainio\Symfony\FormsIntegration\Templating\Helper\TranslatorHelper;
use Mainio\Symfony\FormsIntegration\Templating\Loader\FilesystemLoader;
use Symfony\Component\Form\FormRenderer;
use Symfony\Component\Form\Forms;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Templating\PhpEngine;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
$basedir = dirname(__DIR__);
require_once $basedir . '/vendor/autoload.php';
$loader = new FilesystemLoader(array());
$engine = new PhpEngine(new TemplateNameParser(), $loader);
$tre = new TemplatingRendererEngine($engine, array(
$basedir . '/src/Resources/views/Form'
));
$renderer = new FormRenderer($tre);
// TODO: Set the translations within the concrete5 context
$translator = new Translator('en_US');
$formHelper = new FormHelper($renderer);
$translatorHelper = new TranslatorHelper($translator);
$engine->addHelpers(array($formHelper, $translatorHelper));
// Generate form view
$validator = Validation::createValidator();
$session = new Session();
$csrfGenerator = new \Symfony\Component\Security\Csrf\TokenGenerator\UriSafeTokenGenerator();
$csrfStorage = new \Symfony\Component\Security\Csrf\TokenStorage\SessionTokenStorage($session);
$csrfManager = new \Symfony\Component\Security\Csrf\CsrfTokenManager($csrfGenerator, $csrfStorage);
$factory = Forms::createFormFactoryBuilder()
->addExtension(new \Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationExtension())
->addExtension(new \Symfony\Component\Form\Extension\Csrf\CsrfExtension($csrfManager))
->addExtension(new \Symfony\Component\Form\Extension\Validator\ValidatorExtension($validator))
->getFormFactory();
$builder = $factory->createBuilder(FormType::class, null, array('action' => '#'))
->add('name', TextType::class)
->add('submit', SubmitType::class, array('label' => 'Hello world!'));
$form = $builder->getForm();
// Render the form view
echo $renderer->renderBlock($form->createView(), 'form');
<?php
namespace Mainio\Symfony\FormsIntegration\Templating\Loader;
use Symfony\Component\Templating\Loader\FilesystemLoader as BaseFilesystemLoader;
use Symfony\Component\Templating\Storage\FileStorage;
use Symfony\Component\Templating\TemplateReferenceInterface;
class FilesystemLoader extends BaseFilesystemLoader
{
/**
* {@inheritdoc}
*/
public function load(TemplateReferenceInterface $template)
{
$file = $template->getPath();
if (self::isAbsolutePath($file) && is_file($file)) {
return new FileStorage($file);
}
$replacements = array();
foreach ($template->all() as $key => $value) {
$replacements['%'.$key.'%'] = $value;
}
$fileFailures = array();
foreach ($this->templatePathPatterns as $templatePathPattern) {
if (is_file($file = strtr($templatePathPattern, $replacements)) && is_readable($file)) {
if (null !== $this->logger) {
$this->logger->debug('Loaded template file.', array('file' => $file));
}
return new FileStorage($file);
}
if (null !== $this->logger) {
$fileFailures[] = $file;
}
}
// only log failures if no template could be loaded at all
foreach ($fileFailures as $file) {
if (null !== $this->logger) {
$this->logger->debug('Failed loading template file.', array('file' => $file));
}
}
return false;
}
}
<?php
/*
* This file is based on the original templating form helper from the symfony
* framework bundle. Basically the same file but outside of the symfony
* framework bundle.
*/
namespace Mainio\Symfony\FormsIntegration\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\Form\FormView;
/**
* FormHelper provides helpers to help display forms.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormHelper extends Helper
{
/**
* @var FormRendererInterface
*/
private $renderer;
/**
* @param FormRendererInterface $renderer
*/
public function __construct(FormRendererInterface $renderer)
{
$this->renderer = $renderer;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'form';
}
/**
* Sets a theme for a given view.
*
* The theme format is "<Bundle>:<Controller>".
*
* @param FormView $view A FormView instance
* @param string|array $themes A theme or an array of theme
*/
public function setTheme(FormView $view, $themes)
{
$this->renderer->setTheme($view, $themes);
}
/**
* Renders the HTML for a form.
*
* Example usage:
*
* <?php echo view['form']->form($form) ?>
*
* You can pass options during the call:
*
* <?php echo view['form']->form($form, array('attr' => array('class' => 'foo'))) ?>
*
* <?php echo view['form']->form($form, array('separator' => '+++++')) ?>
*
* This method is mainly intended for prototyping purposes. If you want to
* control the layout of a form in a more fine-grained manner, you are
* advised to use the other helper methods for rendering the parts of the
* form individually. You can also create a custom form theme to adapt
* the look of the form.
*
* @param FormView $view The view for which to render the form
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function form(FormView $view, array $variables = array())
{
return $this->renderer->renderBlock($view, 'form', $variables);
}
/**
* Renders the form start tag.
*
* Example usage templates:
*
* <?php echo $view['form']->start($form) ?>>
*
* @param FormView $view The view for which to render the start tag
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function start(FormView $view, array $variables = array())
{
return $this->renderer->renderBlock($view, 'form_start', $variables);
}
/**
* Renders the form end tag.
*
* Example usage templates:
*
* <?php echo $view['form']->end($form) ?>>
*
* @param FormView $view The view for which to render the end tag
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function end(FormView $view, array $variables = array())
{
return $this->renderer->renderBlock($view, 'form_end', $variables);
}
/**
* Renders the HTML for a given view.
*
* Example usage:
*
* <?php echo $view['form']->widget($form) ?>
*
* You can pass options during the call:
*
* <?php echo $view['form']->widget($form, array('attr' => array('class' => 'foo'))) ?>
*
* <?php echo $view['form']->widget($form, array('separator' => '+++++')) ?>
*
* @param FormView $view The view for which to render the widget
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function widget(FormView $view, array $variables = array())
{
return $this->renderer->searchAndRenderBlock($view, 'widget', $variables);
}
/**
* Renders the entire form field "row".
*
* @param FormView $view The view for which to render the row
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function row(FormView $view, array $variables = array())
{
return $this->renderer->searchAndRenderBlock($view, 'row', $variables);
}
/**
* Renders the label of the given view.
*
* @param FormView $view The view for which to render the label
* @param string $label The label
* @param array $variables Additional variables passed to the template
*
* @return string The HTML markup
*/
public function label(FormView $view, $label = null, array $variables = array())
{
if (null !== $label) {
$variables += array('label' => $label);
}
return $this->renderer->searchAndRenderBlock($view, 'label', $variables);
}
/**
* Renders the errors of the given view.
*
* @param FormView $view The view to render the errors for
*
* @return string The HTML markup
*/
public function errors(FormView $view)
{
return $this->renderer->searchAndRenderBlock($view, 'errors');
}
/**
* Renders views which have not already been rendered.
*
* @param FormView $view The parent view
* @param array $variables An array of variables
*
* @return string The HTML markup
*/
public function rest(FormView $view, array $variables = array())
{
return $this->renderer->searchAndRenderBlock($view, 'rest', $variables);
}
/**
* Renders a block of the template.
*
* @param FormView $view The view for determining the used themes.
* @param string $blockName The name of the block to render.
* @param array $variables The variable to pass to the template.
*
* @return string The HTML markup
*/
public function block(FormView $view, $blockName, array $variables = array())
{
return $this->renderer->renderBlock($view, $blockName, $variables);
}
/**
* Returns a CSRF token.
*
* Use this helper for CSRF protection without the overhead of creating a
* form.
*
* <code>
* echo $view['form']->csrfToken('rm_user_'.$user->getId());
* </code>
*
* Check the token in your action using the same CSRF token id.
*
* <code>
* $csrfProvider = $this->get('security.csrf.token_generator');
* if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
* throw new \RuntimeException('CSRF attack detected.');
* }
* </code>
*
* @param string $tokenId The CSRF token id of the protected action
*
* @return string A CSRF token
*
* @throws \BadMethodCallException When no CSRF provider was injected in the constructor.
*/
public function csrfToken($tokenId)
{
return $this->renderer->renderCsrfToken($tokenId);
}
public function humanize($text)
{
return $this->renderer->humanize($text);
}
}
<?php
namespace Mainio\Symfony\FormsIntegration;
use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
use Symfony\Component\Templating\TemplateNameParserInterface;
class TemplateNameParser extends BaseTemplateNameParser
{
/**
* {@inheritdoc}
*/
public function parse($name)
{
if ($name instanceof TemplateReferenceInterface) {
return $name;
}
$location = null;
if (false !== $pos = strrpos($name, ':')) {
$location = substr($name, 0, $pos);
$name = substr($name, $pos + 1);
}
$engine = null;
if (false !== $pos = strrpos($name, '.')) {
$engine = substr($name, $pos + 1);
}
return new TemplateReference($location, $name, $engine);
}
}
<?php
namespace Mainio\Symfony\FormsIntegration;
use Symfony\Component\Templating\TemplateReferenceInterface;
class TemplateReference implements TemplateReferenceInterface
{
protected $parameters;
public function __construct($location = null, $name = null, $engine = null)
{
$this->parameters = array(
'location' => $location,
'name' => $name,
'engine' => $engine,
);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->getLogicalName();
}
/**
* {@inheritdoc}
*/
public function set($name, $value)
{
if (array_key_exists($name, $this->parameters)) {
$this->parameters[$name] = $value;
} else {
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
}
return $this;
}
/**
* {@inheritdoc}
*/
public function get($name)
{
if (array_key_exists($name, $this->parameters)) {
return $this->parameters[$name];
}
throw new \InvalidArgumentException(sprintf('The template does not support the "%s" parameter.', $name));
}
/**
* {@inheritdoc}
*/
public function all()
{
return $this->parameters;
}
/**
* {@inheritdoc}
*/
public function getPath()
{
return $this->parameters['location'] . '/' . $this->parameters['name'];
}
/**
* {@inheritdoc}
*/
public function getLogicalName()
{
return $this->parameters['location'] . ':' . $this->parameters['name'];
}
}
<?php
/*
* This file is based on the original templating translator helper from the
* symfony framework bundle. Basically the same file but outside of the symfony
* framework bundle.
*/
namespace Mainio\Symfony\FormsIntegration\Templating\Helper;
use Symfony\Component\Templating\Helper\Helper;
use Symfony\Component\Translation\TranslatorInterface;
/**
* TranslatorHelper.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslatorHelper extends Helper
{
protected $translator;
/**
* Constructor.
*
* @param TranslatorInterface $translator A TranslatorInterface instance
*/
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* @see TranslatorInterface::trans()
*/
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{
return $this->translator->trans($id, $parameters, $domain, $locale);
}
/**
* @see TranslatorInterface::transChoice()
*/
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'translator';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment