Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 290 lines (247 sloc) 8.686 kB
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
1 <?php
2
1c9d761 @blue-eyes normalized license messages in PHP files
blue-eyes authored
3 /*
4 * This file is part of the Symfony package.
5 *
506e5b7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
1c9d761 @blue-eyes normalized license messages in PHP files
blue-eyes authored
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
12 namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
13
14 use Symfony\Component\Templating\Helper\Helper;
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
15 use Symfony\Component\Templating\EngineInterface;
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
16 use Symfony\Component\Form\FormView;
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
17 use Symfony\Component\Form\Exception\FormException;
925e14d @fabpot moved some FormView methods to FormUtil where they really belongs
fabpot authored
18 use Symfony\Component\Form\Util\FormUtil;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
19
20 /**
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
21 *
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
22 *
506e5b7 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
23 * @author Fabien Potencier <fabien@symfony.com>
24 * @author Bernhard Schussek <bernhard.schussek@symfony.com>
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
25 */
26 class FormHelper extends Helper
27 {
28 static protected $cache = array();
29
30 protected $engine;
31
b0a6506 @webmozart [Form] Fixed variable scope when entering nested form helpers
webmozart authored
32 protected $varStack;
33
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
34 protected $context;
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
35
1b82ece @stfalcon fix bug in Symfony\Bundle\FrameworkBundle\Templating\Helper\FormHelper
stfalcon authored
36 public function __construct(EngineInterface $engine)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
37 {
38 $this->engine = $engine;
60711af @vicb [Form] Add the ability to override label & widget options when render…
vicb authored
39 $this->varStack = array();
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
40 $this->context = array();
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
41 }
42
925e14d @fabpot moved some FormView methods to FormUtil where they really belongs
fabpot authored
43 public function isChoiceGroup($label)
44 {
45 return FormUtil::isChoiceGroup($label);
46 }
47
48 public function isChoiceSelected(FormView $view, $choice)
49 {
50 return FormUtil::isChoiceSelected($choice, $view->get('value'));
51 }
52
469016f @vicb [FrameworkBundle][Form] Fix label rendering
vicb authored
53 /**
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
54 * Renders the HTML enctype in the form tag, if necessary.
55 *
5daa2b4 @vicb [FrameworkBundle] Fix the FormHelper phpDoc
vicb authored
56 * Example usage templates:
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
57 *
58 * <form action="..." method="post" <?php echo $view['form']->enctype() ?>>
59 *
60 * @param FormView $view The view for which to render the encoding type
61 *
62 * @return string The html markup
63 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
64 public function enctype(FormView $view)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
65 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
66 return $this->renderSection($view, 'enctype');
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
67 }
68
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
69 /**
70 * Renders the HTML for a given view.
71 *
5daa2b4 @vicb [FrameworkBundle] Fix the FormHelper phpDoc
vicb authored
72 * Example usage:
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
73 *
5daa2b4 @vicb [FrameworkBundle] Fix the FormHelper phpDoc
vicb authored
74 * <?php echo view['form']->widget() ?>
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
75 *
76 * You can pass options during the call:
77 *
5daa2b4 @vicb [FrameworkBundle] Fix the FormHelper phpDoc
vicb authored
78 * <?php echo view['form']->widget(array('attr' => array('class' => 'foo'))) ?>
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
79 *
5daa2b4 @vicb [FrameworkBundle] Fix the FormHelper phpDoc
vicb authored
80 * <?php echo view['form']->widget(array('separator' => '+++++)) ?>
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
81 *
82 * @param FormView $view The view for which to render the widget
83 * @param array $variables Additional variables passed to the template
84 *
85 * @return string The html markup
86 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
87 public function widget(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
88 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
89 return trim($this->renderSection($view, 'widget', $variables));
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
90 }
91
92 /**
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
93 * Renders the entire form field "row".
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
94 *
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
95 * @param FormView $view The view for which to render the row
96 * @param array $variables Additional variables passed to the template
c4c66ff @pborreli [Phpdoc] Cleaning/fixing
pborreli authored
97 *
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
98 * @return string The html markup
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
99 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
100 public function row(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
101 {
60711af @vicb [Form] Add the ability to override label & widget options when render…
vicb authored
102 $variables = array_replace_recursive(
103 array(
104 'label' => array(),
105 'widget' => array(),
106 'attr' => array()
107 ),
108 $variables
109 );
a7356b4 @vicb [Form] Optimize rendering
vicb authored
110
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
111 return $this->renderSection($view, 'row', $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
112 }
113
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
114 /**
115 * Renders the label of the given view.
116 *
117 * @param FormView $view The view for which to render the label
118 * @param string $label The label
119 * @param array $variables Additional variables passed to the template
120 *
121 * @return string The html markup
122 */
657e00a @stloyd [Form] Added missing feature for adding attributes to an field label
stloyd authored
123 public function label(FormView $view, $label = null, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
124 {
657e00a @stloyd [Form] Added missing feature for adding attributes to an field label
stloyd authored
125 if ($label !== null) {
126 $variables += array('label' => $label);
127 }
128
129 return $this->renderSection($view, 'label', $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
130 }
131
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
132 /**
133 * Renders the errors of the given view.
134 *
135 * @param FormView $view The view to render the errors for
136 *
137 * @return string The html markup
138 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
139 public function errors(FormView $view)
c43d690 @weaverryan [Form] Adding a row() PHP helper equivalent to the Twig form_row() fo…
weaverryan authored
140 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
141 return $this->renderSection($view, 'errors');
c43d690 @weaverryan [Form] Adding a row() PHP helper equivalent to the Twig form_row() fo…
weaverryan authored
142 }
143
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
144 /**
145 * Renders views which have not already been rendered.
146 *
147 * @param FormView $view The parent view
148 * @param array $variables An array of variables
149 *
150 * @return string The html markup
151 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
152 public function rest(FormView $view, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
153 {
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
154 return $this->renderSection($view, 'rest', $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
155 }
156
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
157 /**
158 * Renders a template.
159 *
160 * 1. This function first looks for a block named "_<view id>_<section>",
161 * 2. if such a block is not found the function will look for a block named
162 * "<type name>_<section>",
163 * 3. the type name is recursively replaced by the parent type name until a
164 * corresponding block is found
165 *
166 * @param FormView $view The form view
167 * @param string $section The section to render (i.e. 'row', 'widget', 'label', ...)
168 * @param array $variables Additional variables
169 *
170 * @return string The html markup
171 *
172 * @throws FormException if no template block exists to render the given section of the view
173 */
13a9cf7 @webmozart [Form] Renamed TemplateContext to FormView
webmozart authored
174 protected function renderSection(FormView $view, $section, array $variables = array())
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
175 {
de24195 @vicb [Form] Never render a view again
vicb authored
176 $mainTemplate = in_array($section, array('row', 'widget'));
177 if ($mainTemplate && $view->isRendered()) {
178
179 return '';
180 }
181
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
182 $template = null;
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
183
184 $custom = '_'.$view->get('proto_id', $view->get('id'));
185 $rendering = $custom.$section;
a259d44 @webmozart [Form] Added tests for blocks/templates in the format _<ID>_(widget|r…
webmozart authored
186
a7356b4 @vicb [Form] Optimize rendering
vicb authored
187 if (isset($this->varStack[$rendering])) {
188 $typeIndex = $this->varStack[$rendering]['typeIndex'] - 1;
189 $types = $this->varStack[$rendering]['types'];
190 $this->varStack[$rendering]['variables'] = array_replace_recursive($this->varStack[$rendering]['variables'], $variables);
191 } else {
192 $types = $view->get('types');
193 $types[] = $custom;
194 $typeIndex = count($types) - 1;
195 $this->varStack[$rendering] = array (
196 'variables' => array_replace_recursive($view->all(), $variables),
197 'types' => $types,
198 );
199 }
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
200
201 do {
c22a6bb @vicb [Form] Fix the exception message when no block is found while rendering
vicb authored
202 $types[$typeIndex] .= '_'.$section;
203 $template = $this->lookupTemplate($types[$typeIndex]);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
204
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
205 if ($template) {
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
206
a7356b4 @vicb [Form] Optimize rendering
vicb authored
207 $this->varStack[$rendering]['typeIndex'] = $typeIndex;
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
208
a7356b4 @vicb [Form] Optimize rendering
vicb authored
209 $this->context[] = $this->varStack[$rendering]['variables'];
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
210
a7356b4 @vicb [Form] Optimize rendering
vicb authored
211 $html = $this->engine->render($template, $this->varStack[$rendering]['variables']);
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
212
213 array_pop($this->context);
a7356b4 @vicb [Form] Optimize rendering
vicb authored
214 unset($this->varStack[$rendering]);
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
215
8c59b3c @vicb [Order] Make Form::types and FormView::types use the same order (Pare…
vicb authored
216 if ($mainTemplate) {
217 $view->setRendered();
218 }
de24195 @vicb [Form] Never render a view again
vicb authored
219
8c59b3c @vicb [Order] Make Form::types and FormView::types use the same order (Pare…
vicb authored
220 return $html;
221 }
a7356b4 @vicb [Form] Optimize rendering
vicb authored
222 } while (--$typeIndex >= 0);
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
223
c22a6bb @vicb [Form] Fix the exception message when no block is found while rendering
vicb authored
224 throw new FormException(sprintf(
225 'Unable to render the form as none of the following blocks exist: "%s".',
226 implode('", "', array_reverse($types))
227 ));
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
228 }
229
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
230 /**
231 * Render a block from a form element.
232 *
233 * @param string $name
234 * @param array $variables Additional variables (those would override the current context)
235 *
236 * @throws FormException if the block is not found
237 * @throws FormException if the method is called out of a form element (no context)
238 */
239 public function renderBlock($name, $variables = array())
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
240 {
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
241 if (0 == count($this->context)) {
242 throw new FormException(sprintf('This method should only be called while rendering a form element.', $name));
243 }
b0a6506 @webmozart [Form] Fixed variable scope when entering nested form helpers
webmozart authored
244
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
245 $template = $this->lookupTemplate($name);
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
246
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
247 if (false === $template) {
248 throw new FormException(sprintf('No block "%s" found while rendering the form.', $name));
249 }
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
250
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
251 $variables = array_replace_recursive(end($this->context), $variables);
bf13576 @webmozart [FrameworkBundle] Added test coverage for FormHelper and fixed variou…
webmozart authored
252
65c41c4 @vicb [Form][FrameworkBundle] Make FormHelper::renderSection() recursively …
vicb authored
253 return $this->engine->render($template, $variables);
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
254 }
255
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
256 /**
257 * Returns the name of the template to use to render the block
258 *
259 * @param string $blockName The name of the block
260 *
261 * @return string|Boolean The template logical name or false when no template is found
262 */
263 protected function lookupTemplate($blockName)
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
264 {
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
265 if (isset(self::$cache[$blockName])) {
266 return self::$cache[$blockName];
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
267 }
268
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
269 $template = $blockName.'.html.php';
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
270 /*
271 if ($this->templateDir) {
272 $template = $this->templateDir.':'.$template;
273 }
274 */
f1331fb @vicb [Twig][Form] Optimize form rendering
vicb authored
275 $template = 'FrameworkBundle:Form:'.$template;
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
276 if (!$this->engine->exists($template)) {
277 $template = false;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
278 }
279
e0f307c @vicb [FrameworkBundle][Form] Add some phpDoc for the FormHelper class
vicb authored
280 self::$cache[$blockName] = $template;
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
281
282 return $template;
283 }
94aae9c @fabpot [Form] reverted the templating part to be similar to what we have today
fabpot authored
284
285 public function getName()
286 {
287 return 'form';
288 }
92b136b @webmozart [Form][FrameworkBundle][TwigBundle] Refactored the PHP and Twig templ…
webmozart authored
289 }
Something went wrong with that request. Please try again.