Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • 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 (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Rendering;
  9: 
 10: use Nette,
 11:     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:      *    /--- if (form.errors) 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:      *            .... if (control.errors) error.container
 51:      *          \---
 52:      *        \---
 53:      *      \---
 54:      *    \---
 55:      *  \--
 56:      *
 57:      * @var array of HTML tags */
 58:     public $wrappers = array(
 59:         'form' => array(
 60:             'container' => NULL,
 61:             'errors' => TRUE,
 62:         ),
 63: 
 64:         'error' => array(
 65:             'container' => 'ul class=error',
 66:             'item' => 'li',
 67:         ),
 68: 
 69:         'group' => array(
 70:             'container' => 'fieldset',
 71:             'label' => 'legend',
 72:             'description' => 'p',
 73:         ),
 74: 
 75:         'controls' => array(
 76:             'container' => 'table',
 77:         ),
 78: 
 79:         'pair' => array(
 80:             'container' => 'tr',
 81:             '.required' => 'required',
 82:             '.optional' => NULL,
 83:             '.odd' => NULL,
 84:         ),
 85: 
 86:         'control' => array(
 87:             'container' => 'td',
 88:             '.odd' => NULL,
 89: 
 90:             'errors' => FALSE,
 91:             'description' => 'small',
 92:             'requiredsuffix' => '',
 93: 
 94:             '.required' => 'required',
 95:             '.text' => 'text',
 96:             '.password' => 'text',
 97:             '.file' => 'text',
 98:             '.submit' => 'button',
 99:             '.image' => 'imagebutton',
100:             '.button' => 'button',
101:         ),
102: 
103:         'label' => array(
104:             'container' => 'th',
105:             'suffix' => NULL,
106:             'requiredsuffix' => '',
107:         ),
108: 
109:         'hidden' => array(
110:             'container' => 'div',
111:         ),
112:     );
113: 
114:     /** @var Nette\Forms\Form */
115:     protected $form;
116: 
117:     /** @var int */
118:     protected $counter;
119: 
120: 
121:     /**
122:      * Provides complete form rendering.
123:      * @param  Nette\Forms\Form
124:      * @param  string 'begin', 'errors', 'body', 'end' or empty to render all
125:      * @return string
126:      */
127:     public function render(Nette\Forms\Form $form, $mode = NULL)
128:     {
129:         if ($this->form !== $form) {
130:             $this->form = $form;
131:             $this->init();
132:         }
133: 
134:         $s = '';
135:         if (!$mode || $mode === 'begin') {
136:             $s .= $this->renderBegin();
137:         }
138:         if ((!$mode && $this->getValue('form errors')) || $mode === 'errors') {
139:             $s .= $this->renderErrors();
140:         }
141:         if (!$mode || $mode === 'body') {
142:             $s .= $this->renderBody();
143:         }
144:         if (!$mode || $mode === 'end') {
145:             $s .= $this->renderEnd();
146:         }
147:         return $s;
148:     }
149: 
150: 
151:     /** @deprecated */
152:     public function setClientScript()
153:     {
154:         trigger_error(__METHOD__ . '() is deprecated; use unobstructive JavaScript instead.', E_USER_WARNING);
155:         return $this;
156:     }
157: 
158: 
159:     /**
160:      * Initializes form.
161:      * @return void
162:      */
163:     protected function init()
164:     {
165:         $wrapper = & $this->wrappers['control'];
166:         foreach ($this->form->getControls() as $control) {
167:             if ($control->isRequired() && isset($wrapper['.required'])) {
168:                 $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
169:             }
170: 
171:             $el = $control->getControlPrototype();
172:             if ($el->getName() === 'input' && isset($wrapper['.' . $el->type])) {
173:                 $el->class($wrapper['.' . $el->type], TRUE);
174:             }
175:         }
176:     }
177: 
178: 
179:     /**
180:      * Renders form begin.
181:      * @return string
182:      */
183:     public function renderBegin()
184:     {
185:         $this->counter = 0;
186: 
187:         foreach ($this->form->getControls() as $control) {
188:             $control->setOption('rendered', FALSE);
189:         }
190: 
191:         if (strcasecmp($this->form->getMethod(), 'get') === 0) {
192:             $el = clone $this->form->getElementPrototype();
193:             $url = explode('?', (string) $el->action, 2);
194:             $el->action = $url[0];
195:             $s = '';
196:             if (isset($url[1])) {
197:                 foreach (preg_split('#[;&]#', $url[1]) as $param) {
198:                     $parts = explode('=', $param, 2);
199:                     $name = urldecode($parts[0]);
200:                     if (!isset($this->form[$name])) {
201:                         $s .= Html::el('input', array('type' => 'hidden', 'name' => $name, 'value' => urldecode($parts[1])));
202:                     }
203:                 }
204:                 $s = "\n\t" . $this->getWrapper('hidden container')->setHtml($s);
205:             }
206:             return $el->startTag() . $s;
207: 
208: 
209:         } else {
210:             return $this->form->getElementPrototype()->startTag();
211:         }
212:     }
213: 
214: 
215:     /**
216:      * Renders form end.
217:      * @return string
218:      */
219:     public function renderEnd()
220:     {
221:         $s = '';
222:         foreach ($this->form->getControls() as $control) {
223:             if ($control instanceof Nette\Forms\Controls\HiddenField && !$control->getOption('rendered')) {
224:                 $s .= (string) $control->getControl();
225:             }
226:         }
227:         if (iterator_count($this->form->getComponents(TRUE, 'Nette\Forms\Controls\TextInput')) < 2) {
228:             $s .= '<!--[if IE]><input type=IEbug disabled style="display:none"><![endif]-->';
229:         }
230:         if ($s) {
231:             $s = $this->getWrapper('hidden container')->setHtml($s) . "\n";
232:         }
233: 
234:         return $s . $this->form->getElementPrototype()->endTag() . "\n";
235:     }
236: 
237: 
238:     /**
239:      * Renders validation errors (per form or per control).
240:      * @return string
241:      */
242:     public function renderErrors(Nette\Forms\IControl $control = NULL)
243:     {
244:         $errors = $control === NULL ? $this->form->getErrors() : $control->getErrors();
245:         if (count($errors)) {
246:             $ul = $this->getWrapper('error container');
247:             $li = $this->getWrapper('error item');
248: 
249:             foreach ($errors as $error) {
250:                 $item = clone $li;
251:                 if ($error instanceof Html) {
252:                     $item->add($error);
253:                 } else {
254:                     $item->setText($error);
255:                 }
256:                 $ul->add($item);
257:             }
258:             return "\n" . $ul->render(0);
259:         }
260:     }
261: 
262: 
263:     /**
264:      * Renders form body.
265:      * @return string
266:      */
267:     public function renderBody()
268:     {
269:         $s = $remains = '';
270: 
271:         $defaultContainer = $this->getWrapper('group container');
272:         $translator = $this->form->getTranslator();
273: 
274:         foreach ($this->form->getGroups() as $group) {
275:             if (!$group->getControls() || !$group->getOption('visual')) {
276:                 continue;
277:             }
278: 
279:             $container = $group->getOption('container', $defaultContainer);
280:             $container = $container instanceof Html ? clone $container : Html::el($container);
281: 
282:             $s .= "\n" . $container->startTag();
283: 
284:             $text = $group->getOption('label');
285:             if ($text instanceof Html) {
286:                 $s .= $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|FormGroup
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 FormContainer or FormGroup 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->getOption('class'), TRUE);
377:         if (++$this->counter % 2) {
378:             $pair->class($this->getValue('pair .odd'), TRUE);
379:         }
380:         $pair->id = $control->getOption('id');
381:         return $pair->render(0);
382:     }
383: 
384: 
385:     /**
386:      * Renders single visual row of multiple controls.
387:      * @param  IFormControl[]
388:      * @return string
389:      */
390:     public function renderPairMulti(array $controls)
391:     {
392:         $s = array();
393:         foreach ($controls as $control) {
394:             if (!$control instanceof Nette\Forms\IControl) {
395:                 throw new Nette\InvalidArgumentException('Argument must be array of IFormControl instances.');
396:             }
397:             $s[] = (string) $control->getControl();
398:         }
399:         $pair = $this->getWrapper('pair container');
400:         $pair->add($this->renderLabel($control));
401:         $pair->add($this->getWrapper('control container')->setHtml(implode(' ', $s)));
402:         return $pair->render(0);
403:     }
404: 
405: 
406:     /**
407:      * Renders 'label' part of visual row of controls.
408:      * @return string
409:      */
410:     public function renderLabel(Nette\Forms\IControl $control)
411:     {
412:         $head = $this->getWrapper('label container');
413: 
414:         if ($control instanceof Nette\Forms\Controls\Checkbox || $control instanceof Nette\Forms\Controls\Button) {
415:             return $head->setHtml(($head->getName() === 'td' || $head->getName() === 'th') ? '&nbsp;' : '');
416: 
417:         } else {
418:             $label = $control->getLabel();
419:             $suffix = $this->getValue('label suffix') . ($control->isRequired() ? $this->getValue('label requiredsuffix') : '');
420:             if ($label instanceof Html) {
421:                 $label->setHtml($label->getHtml() . $suffix);
422:                 $suffix = '';
423:             }
424:             return $head->setHtml((string) $label . $suffix);
425:         }
426:     }
427: 
428: 
429:     /**
430:      * Renders 'control' part of visual row of controls.
431:      * @return string
432:      */
433:     public function renderControl(Nette\Forms\IControl $control)
434:     {
435:         $body = $this->getWrapper('control container');
436:         if ($this->counter % 2) {
437:             $body->class($this->getValue('control .odd'), TRUE);
438:         }
439: 
440:         $description = $control->getOption('description');
441:         if ($description instanceof Html) {
442:             $description = ' ' . $control->getOption('description');
443: 
444:         } elseif (is_string($description)) {
445:             $description = ' ' . $this->getWrapper('control description')->setText($control->translate($description));
446: 
447:         } else {
448:             $description = '';
449:         }
450: 
451:         if ($control->isRequired()) {
452:             $description = $this->getValue('control requiredsuffix') . $description;
453:         }
454: 
455:         if ($this->getValue('control errors')) {
456:             $description .= $this->renderErrors($control);
457:         }
458: 
459:         if ($control instanceof Nette\Forms\Controls\Checkbox || $control instanceof Nette\Forms\Controls\Button) {
460:             return $body->setHtml((string) $control->getControl() . (string) $control->getLabel() . $description);
461: 
462:         } else {
463:             return $body->setHtml((string) $control->getControl() . $description);
464:         }
465:     }
466: 
467: 
468:     /**
469:      * @param  string
470:      * @return Nette\Utils\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.0 API documentation generated by ApiGen 2.8.0