Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

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:             $s .= "\n" . $container->startTag();
278: 
279:             $text = $group->getOption('label');
280:             if ($text instanceof Html) {
281:                 $s .= $this->getWrapper('group label')->add($text);
282: 
283:             } elseif (is_string($text)) {
284:                 if ($translator !== NULL) {
285:                     $text = $translator->translate($text);
286:                 }
287:                 $s .= "\n" . $this->getWrapper('group label')->setText($text) . "\n";
288:             }
289: 
290:             $text = $group->getOption('description');
291:             if ($text instanceof Html) {
292:                 $s .= $text;
293: 
294:             } elseif (is_string($text)) {
295:                 if ($translator !== NULL) {
296:                     $text = $translator->translate($text);
297:                 }
298:                 $s .= $this->getWrapper('group description')->setText($text) . "\n";
299:             }
300: 
301:             $s .= $this->renderControls($group);
302: 
303:             $remains = $container->endTag() . "\n" . $remains;
304:             if (!$group->getOption('embedNext')) {
305:                 $s .= $remains;
306:                 $remains = '';
307:             }
308:         }
309: 
310:         $s .= $remains . $this->renderControls($this->form);
311: 
312:         $container = $this->getWrapper('form container');
313:         $container->setHtml($s);
314:         return $container->render(0);
315:     }
316: 
317: 
318:     /**
319:      * Renders group of controls.
320:      * @param  Nette\Forms\Container|FormGroup
321:      * @return string
322:      */
323:     public function renderControls($parent)
324:     {
325:         if (!($parent instanceof Nette\Forms\Container || $parent instanceof Nette\Forms\ControlGroup)) {
326:             throw new Nette\InvalidArgumentException('Argument must be FormContainer or FormGroup instance.');
327:         }
328: 
329:         $container = $this->getWrapper('controls container');
330: 
331:         $buttons = NULL;
332:         foreach ($parent->getControls() as $control) {
333:             if ($control->getOption('rendered') || $control instanceof Nette\Forms\Controls\HiddenField || $control->getForm(FALSE) !== $this->form) {
334:                 // skip
335: 
336:             } elseif ($control instanceof Nette\Forms\Controls\Button) {
337:                 $buttons[] = $control;
338: 
339:             } else {
340:                 if ($buttons) {
341:                     $container->add($this->renderPairMulti($buttons));
342:                     $buttons = NULL;
343:                 }
344:                 $container->add($this->renderPair($control));
345:             }
346:         }
347: 
348:         if ($buttons) {
349:             $container->add($this->renderPairMulti($buttons));
350:         }
351: 
352:         $s = '';
353:         if (count($container)) {
354:             $s .= "\n" . $container . "\n";
355:         }
356: 
357:         return $s;
358:     }
359: 
360: 
361:     /**
362:      * Renders single visual row.
363:      * @return string
364:      */
365:     public function renderPair(Nette\Forms\IControl $control)
366:     {
367:         $pair = $this->getWrapper('pair container');
368:         $pair->add($this->renderLabel($control));
369:         $pair->add($this->renderControl($control));
370:         $pair->class($this->getValue($control->isRequired() ? 'pair .required' : 'pair .optional'), TRUE);
371:         $pair->class($control->hasErrors() ? $this->getValue('pair .error') : NULL, TRUE);
372:         $pair->class($control->getOption('class'), TRUE);
373:         if (++$this->counter % 2) {
374:             $pair->class($this->getValue('pair .odd'), TRUE);
375:         }
376:         $pair->id = $control->getOption('id');
377:         return $pair->render(0);
378:     }
379: 
380: 
381:     /**
382:      * Renders single visual row of multiple controls.
383:      * @param  IFormControl[]
384:      * @return string
385:      */
386:     public function renderPairMulti(array $controls)
387:     {
388:         $s = array();
389:         foreach ($controls as $control) {
390:             if (!$control instanceof Nette\Forms\IControl) {
391:                 throw new Nette\InvalidArgumentException('Argument must be array of IFormControl instances.');
392:             }
393:             $description = $control->getOption('description');
394:             if ($description instanceof Html) {
395:                 $description = ' ' . $control->getOption('description');
396: 
397:             } elseif (is_string($description)) {
398:                 $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
399: 
400:             } else {
401:                 $description = '';
402:             }
403: 
404:             $s[] = $control->getControl() . $description;
405:         }
406:         $pair = $this->getWrapper('pair container');
407:         $pair->add($this->renderLabel($control));
408:         $pair->add($this->getWrapper('control container')->setHtml(implode(' ', $s)));
409:         return $pair->render(0);
410:     }
411: 
412: 
413:     /**
414:      * Renders 'label' part of visual row of controls.
415:      * @return string
416:      */
417:     public function renderLabel(Nette\Forms\IControl $control)
418:     {
419:         $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
420:         $label = $control->getLabel();
421:         if ($label instanceof Html) {
422:             $label->add($suffix);
423:         } elseif ($label != NULL) { // @intentionally ==
424:             $label .= $suffix;
425:         }
426:         return $this->getWrapper('label container')->setHtml($label);
427:     }
428: 
429: 
430:     /**
431:      * Renders 'control' part of visual row of controls.
432:      * @return string
433:      */
434:     public function renderControl(Nette\Forms\IControl $control)
435:     {
436:         $body = $this->getWrapper('control container');
437:         if ($this->counter % 2) {
438:             $body->class($this->getValue('control .odd'), TRUE);
439:         }
440: 
441:         $description = $control->getOption('description');
442:         if ($description instanceof Html) {
443:             $description = ' ' . $description;
444: 
445:         } elseif (is_string($description)) {
446:             $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
447: 
448:         } else {
449:             $description = '';
450:         }
451: 
452:         if ($control->isRequired()) {
453:             $description = $this->getValue('control requiredsuffix') . $description;
454:         }
455: 
456:         $el = $control->getControl();
457:         return $body->setHtml($el . $description . $this->renderErrors($control));
458:     }
459: 
460: 
461:     /**
462:      * @param  string
463:      * @return Nette\Utils\Html
464:      */
465:     protected function getWrapper($name)
466:     {
467:         $data = $this->getValue($name);
468:         return $data instanceof Html ? clone $data : Html::el($data);
469:     }
470: 
471: 
472:     /**
473:      * @param  string
474:      * @return string
475:      */
476:     protected function getValue($name)
477:     {
478:         $name = explode(' ', $name);
479:         $data = & $this->wrappers[$name[0]][$name[1]];
480:         return $data;
481:     }
482: 
483: }
484: 
Nette 2.1 API documentation generated by ApiGen 2.8.0