123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?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\Bundle\FrameworkBundle\Templating\Helper;
- use Symfony\Component\Form\FormRendererInterface;
- use Symfony\Component\Form\FormView;
- use Symfony\Component\Templating\Helper\Helper;
- /**
- * FormHelper provides helpers to help display forms.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Bernhard Schussek <bschussek@gmail.com>
- */
- class FormHelper extends Helper
- {
- private $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 enctype in the form tag, if necessary.
- *
- * Example usage templates:
- *
- * <form action="..." method="post" <?php echo $view['form']->enctype($form) ?>>
- *
- * @param FormView $view The view for which to render the encoding type
- *
- * @return string The HTML markup
- *
- * @deprecated since version 2.3, to be removed in 3.0.
- * Use {@link start} instead.
- */
- public function enctype(FormView $view)
- {
- @trigger_error('The form helper $view[\'form\']->enctype() is deprecated since Symfony 2.3 and will be removed in 3.0. Use $view[\'form\']->start() instead.', E_USER_DEPRECATED);
- return $this->renderer->searchAndRenderBlock($view, 'enctype');
- }
- /**
- * 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.
- *
- * @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.
- *
- * echo $view['form']->csrfToken('rm_user_'.$user->getId());
- *
- * Check the token in your action using the same intention.
- *
- * $csrfProvider = $this->get('security.csrf.token_generator');
- * if (!$csrfProvider->isCsrfTokenValid('rm_user_'.$user->getId(), $token)) {
- * throw new \RuntimeException('CSRF attack detected.');
- * }
- *
- * @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);
- }
- /**
- * @internal
- */
- public function formEncodeCurrency($text, $widget = '')
- {
- if ('UTF-8' === $charset = $this->getCharset()) {
- $text = htmlspecialchars($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
- } else {
- $text = htmlentities($text, ENT_QUOTES | (\defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0), 'UTF-8');
- $text = iconv('UTF-8', $charset, $text);
- $widget = iconv('UTF-8', $charset, $widget);
- }
- return str_replace('{{ widget }}', $widget, $text);
- }
- }
|