Permalink
Browse files

[Form] Added entry point "Forms" for more convenient usage outside of…

… Symfony
  • Loading branch information...
1 parent 73f3ce3 commit bf274bbcdb0c73e3a522e084f7abe99896a0ddd7 @webmozart webmozart committed Jul 30, 2012
View
@@ -177,3 +177,4 @@ CHANGELOG
* made FormView properties public and deprecated their accessor methods
* made the normalized data of a form accessible in the template through the variable "form.vars.data"
* made the original data of a choice accessible in the template through the property "choice.data"
+ * added convenience class Forms and FormFactoryBuilderInterface
@@ -27,7 +27,7 @@ class CsrfExtension extends AbstractExtension
*
* @param CsrfProviderInterface $csrfProvider The CSRF provider
*/
- public function __construct(CsrfProviderInterface $csrfProvider)
+ public function __construct(CsrfProviderInterface $csrfProvider = null)
{
$this->csrfProvider = $csrfProvider;
}
@@ -0,0 +1,208 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Form\Extension\Templating;
+
+use Symfony\Component\Templating\Helper\Helper;
+use Symfony\Component\Form\FormRendererInterface;
+use Symfony\Component\Form\FormView;
+use Symfony\Component\Templating\EngineInterface;
+use Symfony\Component\Form\Exception\FormException;
+use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
+use Symfony\Component\Form\Extension\Core\View\ChoiceView;
+use Symfony\Component\Form\Util\FormUtil;
+
+/**
+ * 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 enctype in the form tag, if necessary.
+ *
+ * Example usage templates:
+ *
+ * <form action="..." method="post" <?php echo $view['form']->enctype() ?>>
+ *
+ * @param FormView $view The view for which to render the encoding type
+ *
+ * @return string The HTML markup
+ */
+ public function enctype(FormView $view)
+ {
+ return $this->renderer->searchAndRenderBlock($view, 'enctype');
+ }
+
+ /**
+ * Renders the HTML for a given view.
+ *
+ * Example usage:
+ *
+ * <?php echo view['form']->widget() ?>
+ *
+ * You can pass options during the call:
+ *
+ * <?php echo view['form']->widget(array('attr' => array('class' => 'foo'))) ?>
+ *
+ * <?php echo view['form']->widget(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 intention.
+ *
+ * <code>
+ * $csrfProvider = $this->get('form.csrf_provider');
+ * if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
+ * throw new \RuntimeException('CSRF attack detected.');
+ * }
+ * </code>
+ *
+ * @param string $intention The intention of the protected action
+ *
+ * @return string A CSRF token
+ *
+ * @throws \BadMethodCallException When no CSRF provider was injected in the constructor.
+ */
+ public function csrfToken($intention)
+ {
+ return $this->renderer->renderCsrfToken($intention);
+ }
+
+ public function humanize($text)
+ {
+ return $this->renderer->humanize($text);
+ }
+}
@@ -17,6 +17,11 @@
use Symfony\Component\Validator\ValidatorInterface;
use Symfony\Component\Validator\Constraints\Valid;
+/**
+ * Extension supporting the Symfony2 Validator component in forms.
+ *
+ * @author Bernhard Schussek <bschussek@gmail.com>
+ */
class ValidatorExtension extends AbstractExtension
{
private $validator;
@@ -25,6 +30,12 @@ public function __construct(ValidatorInterface $validator)
{
$this->validator = $validator;
+ // Register the form constraints in the validator programmatically.
+ // This functionality is required when using the Form component without
+ // the DIC, where the XML file is loaded automatically. Thus the following
+ // code must be kept synchronized with validation.xml
+
+ /** @var \Symfony\Component\Validator\Mapping\ClassMetadata $metadata */
$metadata = $this->validator->getMetadataFactory()->getClassMetadata('Symfony\Component\Form\Form');
$metadata->addConstraint(new Form());
$metadata->addPropertyConstraint('children', new Valid());
View
@@ -37,15 +37,15 @@ public function __construct(FormRegistryInterface $registry, ResolvedFormTypeFac
/**
* {@inheritdoc}
*/
- public function create($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
+ public function create($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
return $this->createBuilder($type, $data, $options, $parent)->getForm();
}
/**
* {@inheritdoc}
*/
- public function createNamed($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
+ public function createNamed($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
return $this->createNamedBuilder($name, $type, $data, $options, $parent)->getForm();
}
@@ -61,7 +61,7 @@ public function createForProperty($class, $property, $data = null, array $option
/**
* {@inheritdoc}
*/
- public function createBuilder($type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
+ public function createBuilder($type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
$name = $type instanceof FormTypeInterface || $type instanceof ResolvedFormTypeInterface
? $type->getName()
@@ -73,7 +73,7 @@ public function createBuilder($type, $data = null, array $options = array(), For
/**
* {@inheritdoc}
*/
- public function createNamedBuilder($name, $type, $data = null, array $options = array(), FormBuilderInterface $parent = null)
+ public function createNamedBuilder($name, $type = 'form', $data = null, array $options = array(), FormBuilderInterface $parent = null)
{
if (null !== $data && !array_key_exists('data', $options)) {
$options['data'] = $data;
Oops, something went wrong.

0 comments on commit bf274bb

Please sign in to comment.