FormHelper.php 8.4 KB

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