Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • DefaultFormRenderer
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Rendering;
  9: 
 10: use Nette;
 11: use Nette\Utils\Html;
 12: 
 13: 
 14: /**
 15:  * Converts a Form into the HTML output.
 16:  *
 17:  * @author     David Grudl
 18:  */
 19: class DefaultFormRenderer extends Nette\Object implements Nette\Forms\IFormRenderer
 20: {
 21:     /**
 22:      *  /--- form.container
 23:      *
 24:      *    /--- error.container
 25:      *      .... error.item [.class]
 26:      *    \---
 27:      *
 28:      *    /--- hidden.container
 29:      *      .... HIDDEN CONTROLS
 30:      *    \---
 31:      *
 32:      *    /--- group.container
 33:      *      .... group.label
 34:      *      .... group.description
 35:      *
 36:      *      /--- controls.container
 37:      *
 38:      *        /--- pair.container [.required .optional .odd]
 39:      *
 40:      *          /--- label.container
 41:      *            .... LABEL
 42:      *            .... label.suffix
 43:      *            .... label.requiredsuffix
 44:      *          \---
 45:      *
 46:      *          /--- control.container [.odd]
 47:      *            .... CONTROL [.required .text .password .file .submit .button]
 48:      *            .... control.requiredsuffix
 49:      *            .... control.description
 50:      *            .... control.errorcontainer + control.erroritem
 51:      *          \---
 52:      *        \---
 53:      *      \---
 54:      *    \---
 55:      *  \--
 56:      *
 57:      * @var array of HTML tags */
 58:     public $wrappers = array(
 59:         'form' => array(
 60:             'container' => NULL,
 61:         ),
 62: 
 63:         'error' => array(
 64:             'container' => 'ul class=error',
 65:             'item' => 'li',
 66:         ),
 67: 
 68:         'group' => array(
 69:             'container' => 'fieldset',
 70:             'label' => 'legend',
 71:             'description' => 'p',
 72:         ),
 73: 
 74:         'controls' => array(
 75:             'container' => 'table',
 76:         ),
 77: 
 78:         'pair' => array(
 79:             'container' => 'tr',
 80:             '.required' => 'required',
 81:             '.optional' => NULL,
 82:             '.odd' => NULL,
 83:             '.error' => NULL,
 84:         ),
 85: 
 86:         'control' => array(
 87:             'container' => 'td',
 88:             '.odd' => NULL,
 89: 
 90:             'description' => 'small',
 91:             'requiredsuffix' => '',
 92:             'errorcontainer' => 'span class=error',
 93:             'erroritem' => '',
 94: 
 95:             '.required' => 'required',
 96:             '.text' => 'text',
 97:             '.password' => 'text',
 98:             '.file' => 'text',
 99:             '.submit' => 'button',
100:             '.image' => 'imagebutton',
101:             '.button' => 'button',
102:         ),
103: 
104:         'label' => array(
105:             'container' => 'th',
106:             'suffix' => NULL,
107:             'requiredsuffix' => '',
108:         ),
109: 
110:         'hidden' => array(
111:             'container' => 'div',
112:         ),
113:     );
114: 
115:     /** @var Nette\Forms\Form */
116:     protected $form;
117: 
118:     /** @var int */
119:     protected $counter;
120: 
121: 
122:     /**
123:      * Provides complete form rendering.
124:      * @param  Nette\Forms\Form
125:      * @param  string 'begin', 'errors', 'ownerrors', 'body', 'end' or empty to render all
126:      * @return string
127:      */
128:     public function render(Nette\Forms\Form $form, $mode = NULL)
129:     {
130:         if ($this->form !== $form) {
131:             $this->form = $form;
132:             $this->init();
133:         }
134: 
135:         $s = '';
136:         if (!$mode || $mode === 'begin') {
137:             $s .= $this->renderBegin();
138:         }
139:         if (!$mode || strtolower($mode) === 'ownerrors') {
140:             $s .= $this->renderErrors();
141: 
142:         } elseif ($mode === 'errors') {
143:             $s .= $this->renderErrors(NULL, FALSE);
144:         }
145:         if (!$mode || $mode === 'body') {
146:             $s .= $this->renderBody();
147:         }
148:         if (!$mode || $mode === 'end') {
149:             $s .= $this->renderEnd();
150:         }
151:         return $s;
152:     }
153: 
154: 
155:     /**
156:      * Initializes form.
157:      * @return void
158:      */
159:     protected function init()
160:     {
161:         $wrapper = & $this->wrappers['control'];
162:         foreach ($this->form->getControls() as $control) {
163:             if ($control->isRequired() && isset($wrapper['.required'])) {
164:                 $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
165:             }
166: 
167:             $el = $control->getControlPrototype();
168:             if ($el->getName() === 'input' && isset($wrapper['.' . $el->type])) {
169:                 $el->class($wrapper['.' . $el->type], TRUE);
170:             }
171:         }
172:     }
173: 
174: 
175:     /**
176:      * Renders form begin.
177:      * @return string
178:      */
179:     public function renderBegin()
180:     {
181:         $this->counter = 0;
182: 
183:         foreach ($this->form->getControls() as $control) {
184:             $control->setOption('rendered', FALSE);
185:         }
186: 
187:         if (strcasecmp($this->form->getMethod(), 'get') === 0) {
188:             $el = clone $this->form->getElementPrototype();
189:             $query = parse_url($el->action, PHP_URL_QUERY);
190:             $el->action = str_replace("?$query", '', $el->action);
191:             $s = '';
192:             foreach (preg_split('#[;&]#', $query, NULL, PREG_SPLIT_NO_EMPTY) as $param) {
193:                 $parts = explode('=', $param, 2);
194:                 $name = urldecode($parts[0]);
195:                 if (!isset($this->form[$name])) {
196:                     $s .= Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
197:                 }
198:             }
199:             return $el->startTag() . ($s ? "\n\t" . $this->getWrapper('hidden container')->setHtml($s) : '');
200: 
201:         } else {
202:             return $this->form->getElementPrototype()->startTag();
203:         }
204:     }
205: 
206: 
207:     /**
208:      * Renders form end.
209:      * @return string
210:      */
211:     public function renderEnd()
212:     {
213:         $s = '';
214:         foreach ($this->form->getControls() as $control) {
215:             if ($control instanceof Nette\Forms\Controls\HiddenField && !$control->getOption('rendered')) {
216:                 $s .= $control->getControl();
217:             }
218:         }
219:         if (iterator_count($this->form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
220:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
221:         }
222:         if ($s) {
223:             $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
224:         }
225: 
226:         return $s . $this->form->getElementPrototype()->endTag() . "\n";
227:     }
228: 
229: 
230:     /**
231:      * Renders validation errors (per form or per control).
232:      * @return string
233:      */
234:     public function renderErrors(Nette\Forms\IControl $control = NULL, $own = TRUE)
235:     {
236:         $errors = $control
237:             ? $control->getErrors()
238:             : ($own ? $this->form->getOwnErrors() : $this->form->getErrors());
239:         if (!$errors) {
240:             return;
241:         }
242:         $container = $this->getWrapper($control ? 'control errorcontainer' : 'error container');
243:         $item = $this->getWrapper($control ? 'control erroritem' : 'error item');
244: 
245:         foreach ($errors as $error) {
246:             $item = clone $item;
247:             if ($error instanceof Html) {
248:                 $item->add($error);
249:             } else {
250:                 $item->setText($error);
251:             }
252:             $container->add($item);
253:         }
254:         return "\n" . $container->render($control ? 1 : 0);
255:     }
256: 
257: 
258:     /**
259:      * Renders form body.
260:      * @return string
261:      */
262:     public function renderBody()
263:     {
264:         $s = $remains = '';
265: 
266:         $defaultContainer = $this->getWrapper('group container');
267:         $translator = $this->form->getTranslator();
268: 
269:         foreach ($this->form->getGroups() as $group) {
270:             if (!$group->getControls() || !$group->getOption('visual')) {
271:                 continue;
272:             }
273: 
274:             $container = $group->getOption('container', $defaultContainer);
275:             $container = $container instanceof Html ? clone $container : Html::el($container);
276: 
277:             $id = $group->getOption('id');
278:             if ($id) {
279:                 $container->id = $id;
280:             }
281: 
282:             $s .= "\n" . $container->startTag();
283: 
284:             $text = $group->getOption('label');
285:             if ($text instanceof Html) {
286:                 $s .= $this->getWrapper('group label')->add($text);
287: 
288:             } elseif (is_string($text)) {
289:                 if ($translator !== NULL) {
290:                     $text = $translator->translate($text);
291:                 }
292:                 $s .= "\n" . $this->getWrapper('group label')->setText($text) . "\n";
293:             }
294: 
295:             $text = $group->getOption('description');
296:             if ($text instanceof Html) {
297:                 $s .= $text;
298: 
299:             } elseif (is_string($text)) {
300:                 if ($translator !== NULL) {
301:                     $text = $translator->translate($text);
302:                 }
303:                 $s .= $this->getWrapper('group description')->setText($text) . "\n";
304:             }
305: 
306:             $s .= $this->renderControls($group);
307: 
308:             $remains = $container->endTag() . "\n" . $remains;
309:             if (!$group->getOption('embedNext')) {
310:                 $s .= $remains;
311:                 $remains = '';
312:             }
313:         }
314: 
315:         $s .= $remains . $this->renderControls($this->form);
316: 
317:         $container = $this->getWrapper('form container');
318:         $container->setHtml($s);
319:         return $container->render(0);
320:     }
321: 
322: 
323:     /**
324:      * Renders group of controls.
325:      * @param  Nette\Forms\Container|Nette\Forms\ControlGroup
326:      * @return string
327:      */
328:     public function renderControls($parent)
329:     {
330:         if (!($parent instanceof Nette\Forms\Container || $parent instanceof Nette\Forms\ControlGroup)) {
331:             throw new Nette\InvalidArgumentException('Argument must be Nette\Forms\Container or Nette\Forms\ControlGroup instance.');
332:         }
333: 
334:         $container = $this->getWrapper('controls container');
335: 
336:         $buttons = NULL;
337:         foreach ($parent->getControls() as $control) {
338:             if ($control->getOption('rendered') || $control instanceof Nette\Forms\Controls\HiddenField || $control->getForm(FALSE) !== $this->form) {
339:                 // skip
340: 
341:             } elseif ($control instanceof Nette\Forms\Controls\Button) {
342:                 $buttons[] = $control;
343: 
344:             } else {
345:                 if ($buttons) {
346:                     $container->add($this->renderPairMulti($buttons));
347:                     $buttons = NULL;
348:                 }
349:                 $container->add($this->renderPair($control));
350:             }
351:         }
352: 
353:         if ($buttons) {
354:             $container->add($this->renderPairMulti($buttons));
355:         }
356: 
357:         $s = '';
358:         if (count($container)) {
359:             $s .= "\n" . $container . "\n";
360:         }
361: 
362:         return $s;
363:     }
364: 
365: 
366:     /**
367:      * Renders single visual row.
368:      * @return string
369:      */
370:     public function renderPair(Nette\Forms\IControl $control)
371:     {
372:         $pair = $this->getWrapper('pair container');
373:         $pair->add($this->renderLabel($control));
374:         $pair->add($this->renderControl($control));
375:         $pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), TRUE);
376:         $pair->class($control->hasErrors() ? $this->getValue('pair .error') : NULL, TRUE);
377:         $pair->class($control->getOption('class'), TRUE);
378:         if (++$this->counter % 2) {
379:             $pair->class($this->getValue('pair .odd'), TRUE);
380:         }
381:         $pair->id = $control->getOption('id');
382:         return $pair->render(0);
383:     }
384: 
385: 
386:     /**
387:      * Renders single visual row of multiple controls.
388:      * @param  Nette\Forms\IControl[]
389:      * @return string
390:      */
391:     public function renderPairMulti(array $controls)
392:     {
393:         $s = array();
394:         foreach ($controls as $control) {
395:             if (!$control instanceof Nette\Forms\IControl) {
396:                 throw new Nette\InvalidArgumentException('Argument must be array of Nette\Forms\IControl instances.');
397:             }
398:             $description = $control->getOption('description');
399:             if ($description instanceof Html) {
400:                 $description = ' ' . $control->getOption('description');
401: 
402:             } elseif (is_string($description)) {
403:                 $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
404: 
405:             } else {
406:                 $description = '';
407:             }
408: 
409:             $control->setOption('rendered', TRUE);
410:             $s[] = $control->getControl() . $description;
411:         }
412:         $pair = $this->getWrapper('pair container');
413:         $pair->add($this->renderLabel($control));
414:         $pair->add($this->getWrapper('control container')->setHtml(implode(' ', $s)));
415:         return $pair->render(0);
416:     }
417: 
418: 
419:     /**
420:      * Renders 'label' part of visual row of controls.
421:      * @return string
422:      */
423:     public function renderLabel(Nette\Forms\IControl $control)
424:     {
425:         $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
426:         $label = $control->getLabel();
427:         if ($label instanceof Html) {
428:             $label->add($suffix);
429:         } elseif ($label != NULL) { // @intentionally ==
430:             $label .= $suffix;
431:         }
432:         return $this->getWrapper('label container')->setHtml($label);
433:     }
434: 
435: 
436:     /**
437:      * Renders 'control' part of visual row of controls.
438:      * @return string
439:      */
440:     public function renderControl(Nette\Forms\IControl $control)
441:     {
442:         $body = $this->getWrapper('control container');
443:         if ($this->counter % 2) {
444:             $body->class($this->getValue('control .odd'), TRUE);
445:         }
446: 
447:         $description = $control->getOption('description');
448:         if ($description instanceof Html) {
449:             $description = ' ' . $description;
450: 
451:         } elseif (is_string($description)) {
452:             $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
453: 
454:         } else {
455:             $description = '';
456:         }
457: 
458:         if ($control->isRequired()) {
459:             $description = $this->getValue('control requiredsuffix') . $description;
460:         }
461: 
462:         $control->setOption('rendered', TRUE);
463:         $el = $control->getControl();
464:         return $body->setHtml($el . $description . $this->renderErrors($control));
465:     }
466: 
467: 
468:     /**
469:      * @param  string
470:      * @return Html
471:      */
472:     protected function getWrapper($name)
473:     {
474:         $data = $this->getValue($name);
475:         return $data instanceof Html ? clone $data : Html::el($data);
476:     }
477: 
478: 
479:     /**
480:      * @param  string
481:      * @return string
482:      */
483:     protected function getValue($name)
484:     {
485:         $name = explode(' ', $name);
486:         $data = & $this->wrappers[$name[0]][$name[1]];
487:         return $data;
488:     }
489: 
490: }
491: 
Nette 2.2 API documentation generated by ApiGen 2.8.0