vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/Helper/FormHelper.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
  11. @trigger_error('The '.FormHelper::class.' class is deprecated since version 4.3 and will be removed in 5.0; use Twig instead.'E_USER_DEPRECATED);
  12. use Symfony\Component\Form\FormRendererInterface;
  13. use Symfony\Component\Form\FormView;
  14. use Symfony\Component\Templating\Helper\Helper;
  15. /**
  16.  * FormHelper provides helpers to help display forms.
  17.  *
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  * @author Bernhard Schussek <bschussek@gmail.com>
  20.  *
  21.  * @deprecated since version 4.3, to be removed in 5.0; use Twig instead.
  22.  */
  23. class FormHelper extends Helper
  24. {
  25.     private $renderer;
  26.     public function __construct(FormRendererInterface $renderer)
  27.     {
  28.         $this->renderer $renderer;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function getName()
  34.     {
  35.         return 'form';
  36.     }
  37.     /**
  38.      * Sets a theme for a given view.
  39.      *
  40.      * The theme format is "<Bundle>:<Controller>".
  41.      *
  42.      * @param FormView     $view             A FormView instance
  43.      * @param string|array $themes           A theme or an array of theme
  44.      * @param bool         $useDefaultThemes If true, will use default themes defined in the renderer
  45.      */
  46.     public function setTheme(FormView $view$themes$useDefaultThemes true)
  47.     {
  48.         $this->renderer->setTheme($view$themes$useDefaultThemes);
  49.     }
  50.     /**
  51.      * Renders the HTML for a form.
  52.      *
  53.      * Example usage:
  54.      *
  55.      *     <?php echo view['form']->form($form) ?>
  56.      *
  57.      * You can pass options during the call:
  58.      *
  59.      *     <?php echo view['form']->form($form, ['attr' => ['class' => 'foo']]) ?>
  60.      *
  61.      *     <?php echo view['form']->form($form, ['separator' => '+++++']) ?>
  62.      *
  63.      * This method is mainly intended for prototyping purposes. If you want to
  64.      * control the layout of a form in a more fine-grained manner, you are
  65.      * advised to use the other helper methods for rendering the parts of the
  66.      * form individually. You can also create a custom form theme to adapt
  67.      * the look of the form.
  68.      *
  69.      * @param FormView $view      The view for which to render the form
  70.      * @param array    $variables Additional variables passed to the template
  71.      *
  72.      * @return string The HTML markup
  73.      */
  74.     public function form(FormView $view, array $variables = [])
  75.     {
  76.         return $this->renderer->renderBlock($view'form'$variables);
  77.     }
  78.     /**
  79.      * Renders the form start tag.
  80.      *
  81.      * Example usage templates:
  82.      *
  83.      *     <?php echo $view['form']->start($form) ?>>
  84.      *
  85.      * @param FormView $view      The view for which to render the start tag
  86.      * @param array    $variables Additional variables passed to the template
  87.      *
  88.      * @return string The HTML markup
  89.      */
  90.     public function start(FormView $view, array $variables = [])
  91.     {
  92.         return $this->renderer->renderBlock($view'form_start'$variables);
  93.     }
  94.     /**
  95.      * Renders the form end tag.
  96.      *
  97.      * Example usage templates:
  98.      *
  99.      *     <?php echo $view['form']->end($form) ?>>
  100.      *
  101.      * @param FormView $view      The view for which to render the end tag
  102.      * @param array    $variables Additional variables passed to the template
  103.      *
  104.      * @return string The HTML markup
  105.      */
  106.     public function end(FormView $view, array $variables = [])
  107.     {
  108.         return $this->renderer->renderBlock($view'form_end'$variables);
  109.     }
  110.     /**
  111.      * Renders the HTML for a given view.
  112.      *
  113.      * Example usage:
  114.      *
  115.      *     <?php echo $view['form']->widget($form) ?>
  116.      *
  117.      * You can pass options during the call:
  118.      *
  119.      *     <?php echo $view['form']->widget($form, ['attr' => ['class' => 'foo']]) ?>
  120.      *
  121.      *     <?php echo $view['form']->widget($form, ['separator' => '+++++']) ?>
  122.      *
  123.      * @param FormView $view      The view for which to render the widget
  124.      * @param array    $variables Additional variables passed to the template
  125.      *
  126.      * @return string The HTML markup
  127.      */
  128.     public function widget(FormView $view, array $variables = [])
  129.     {
  130.         return $this->renderer->searchAndRenderBlock($view'widget'$variables);
  131.     }
  132.     /**
  133.      * Renders the entire form field "row".
  134.      *
  135.      * @param FormView $view      The view for which to render the row
  136.      * @param array    $variables Additional variables passed to the template
  137.      *
  138.      * @return string The HTML markup
  139.      */
  140.     public function row(FormView $view, array $variables = [])
  141.     {
  142.         return $this->renderer->searchAndRenderBlock($view'row'$variables);
  143.     }
  144.     /**
  145.      * Renders the label of the given view.
  146.      *
  147.      * @param FormView $view      The view for which to render the label
  148.      * @param string   $label     The label
  149.      * @param array    $variables Additional variables passed to the template
  150.      *
  151.      * @return string The HTML markup
  152.      */
  153.     public function label(FormView $view$label null, array $variables = [])
  154.     {
  155.         if (null !== $label) {
  156.             $variables += ['label' => $label];
  157.         }
  158.         return $this->renderer->searchAndRenderBlock($view'label'$variables);
  159.     }
  160.     /**
  161.      * Renders the help of the given view.
  162.      *
  163.      * @param FormView $view The parent view
  164.      *
  165.      * @return string The HTML markup
  166.      */
  167.     public function help(FormView $view): string
  168.     {
  169.         return $this->renderer->searchAndRenderBlock($view'help');
  170.     }
  171.     /**
  172.      * Renders the errors of the given view.
  173.      *
  174.      * @return string The HTML markup
  175.      */
  176.     public function errors(FormView $view)
  177.     {
  178.         return $this->renderer->searchAndRenderBlock($view'errors');
  179.     }
  180.     /**
  181.      * Renders views which have not already been rendered.
  182.      *
  183.      * @param FormView $view      The parent view
  184.      * @param array    $variables An array of variables
  185.      *
  186.      * @return string The HTML markup
  187.      */
  188.     public function rest(FormView $view, array $variables = [])
  189.     {
  190.         return $this->renderer->searchAndRenderBlock($view'rest'$variables);
  191.     }
  192.     /**
  193.      * Renders a block of the template.
  194.      *
  195.      * @param FormView $view      The view for determining the used themes
  196.      * @param string   $blockName The name of the block to render
  197.      * @param array    $variables The variable to pass to the template
  198.      *
  199.      * @return string The HTML markup
  200.      */
  201.     public function block(FormView $view$blockName, array $variables = [])
  202.     {
  203.         return $this->renderer->renderBlock($view$blockName$variables);
  204.     }
  205.     /**
  206.      * Returns a CSRF token.
  207.      *
  208.      * Use this helper for CSRF protection without the overhead of creating a
  209.      * form.
  210.      *
  211.      *     echo $view['form']->csrfToken('rm_user_'.$user->getId());
  212.      *
  213.      * Check the token in your action using the same CSRF token id.
  214.      *
  215.      *     // $csrfProvider being an instance of Symfony\Component\Security\Csrf\TokenGenerator\TokenGeneratorInterface
  216.      *     if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
  217.      *         throw new \RuntimeException('CSRF attack detected.');
  218.      *     }
  219.      *
  220.      * @param string $tokenId The CSRF token id of the protected action
  221.      *
  222.      * @return string A CSRF token
  223.      *
  224.      * @throws \BadMethodCallException when no CSRF provider was injected in the constructor
  225.      */
  226.     public function csrfToken($tokenId)
  227.     {
  228.         return $this->renderer->renderCsrfToken($tokenId);
  229.     }
  230.     public function humanize($text)
  231.     {
  232.         return $this->renderer->humanize($text);
  233.     }
  234.     /**
  235.      * @internal
  236.      */
  237.     public function formEncodeCurrency($text$widget '')
  238.     {
  239.         if ('UTF-8' === $charset $this->getCharset()) {
  240.             $text htmlspecialchars($textENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE 0), 'UTF-8');
  241.         } else {
  242.             $text htmlentities($textENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE 0), 'UTF-8');
  243.             $text iconv('UTF-8'$charset$text);
  244.             $widget iconv('UTF-8'$charset$widget);
  245.         }
  246.         return str_replace('{{ widget }}'$widget$text);
  247.     }
  248. }