FormHelper.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Templating\Helper\Helper;
  12. use Symfony\Component\Templating\EngineInterface;
  13. use Symfony\Component\Form\FormView;
  14. use Symfony\Component\Form\Exception\FormException;
  15. /**
  16. *
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Bernhard Schussek <bernhard.schussek@symfony.com>
  20. */
  21. class FormHelper extends Helper
  22. {
  23. static protected $cache = array();
  24. protected $engine;
  25. protected $varStack = array();
  26. public function __construct(EngineInterface $engine)
  27. {
  28. $this->engine = $engine;
  29. }
  30. public function attributes()
  31. {
  32. $html = '';
  33. $attr = array();
  34. if (count($this->varStack) > 0) {
  35. $vars = end($this->varStack);
  36. if (isset($vars['attr'])) {
  37. $attr = $vars['attr'];
  38. }
  39. if (isset($vars['id'])) {
  40. $attr['id'] = $vars['id'];
  41. }
  42. }
  43. foreach ($attr as $k => $v) {
  44. $html .= ' '.$this->engine->escape($k).'="'.$this->engine->escape($v).'"';
  45. }
  46. return $html;
  47. }
  48. public function enctype(FormView $view)
  49. {
  50. return $this->renderSection($view, 'enctype');
  51. }
  52. public function widget(FormView $view, array $variables = array())
  53. {
  54. return trim($this->renderSection($view, 'widget', $variables));
  55. }
  56. /**
  57. * Renders the entire form field "row".
  58. *
  59. * @param FieldInterface $field
  60. * @return string
  61. */
  62. public function row(FormView $view, array $variables = array())
  63. {
  64. return $this->renderSection($view, 'row', $variables);
  65. }
  66. public function label(FormView $view, $label = null)
  67. {
  68. return $this->renderSection($view, 'label', null === $label ? array() : array('label' => $label));
  69. }
  70. public function errors(FormView $view)
  71. {
  72. return $this->renderSection($view, 'errors');
  73. }
  74. public function rest(FormView $view, array $variables = array())
  75. {
  76. return $this->renderSection($view, 'rest', $variables);
  77. }
  78. protected function renderSection(FormView $view, $section, array $variables = array())
  79. {
  80. $template = null;
  81. $blocks = $view->get('types');
  82. foreach ($blocks as &$block) {
  83. $block = $block.'_'.$section;
  84. $template = $this->lookupTemplate($block);
  85. if ($template) {
  86. break;
  87. }
  88. }
  89. if (!$template) {
  90. throw new FormException(sprintf('Unable to render form as none of the following blocks exist: "%s".', implode('", "', $blocks)));
  91. }
  92. if ('widget' === $section || 'row' === $section) {
  93. $view->setRendered(true);
  94. }
  95. return $this->render($template, array_merge($view->all(), $variables));
  96. }
  97. public function render($template, array $variables = array())
  98. {
  99. array_push($this->varStack, array_merge(
  100. count($this->varStack) > 0 ? end($this->varStack) : array(),
  101. $variables
  102. ));
  103. $html = $this->engine->render($template, end($this->varStack));
  104. array_pop($this->varStack);
  105. return $html;
  106. }
  107. protected function lookupTemplate($templateName)
  108. {
  109. if (isset(self::$cache[$templateName])) {
  110. return self::$cache[$templateName];
  111. }
  112. $template = $templateName.'.html.php';
  113. /*
  114. if ($this->templateDir) {
  115. $template = $this->templateDir.':'.$template;
  116. }
  117. */
  118. $template = 'FrameworkBundle:Form:'.$template;
  119. if (!$this->engine->exists($template)) {
  120. $template = false;
  121. }
  122. self::$cache[$templateName] = $template;
  123. return $template;
  124. }
  125. public function getName()
  126. {
  127. return 'form';
  128. }
  129. }