Source for file ConventionalRenderer.php
Documentation is available at ConventionalRenderer.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
17: * @package Nette\Forms
23: require_once dirname(__FILE__) .
'/../../Object.php';
25: require_once dirname(__FILE__) .
'/../../Forms/IFormRenderer.php';
30: * Converts a Form into the HTML output.
32: * @author David Grudl
33: * @copyright Copyright (c) 2004, 2009 David Grudl
34: * @package Nette\Forms
39: * /--- form.container
41: * /--- if (form.errors) error.container
42: * .... error.item [.class]
45: * /--- hidden.container
46: * .... HIDDEN CONTROLS
49: * /--- group.container
51: * .... group.description
53: * /--- controls.container
55: * /--- pair.container [.required .optional .odd]
57: * /--- label.container
62: * /--- control.container [.odd]
63: * .... CONTROL [.required .text .password .file .submit .button]
64: * .... control.description
65: * .... if (control.errors) error.container
72: * @var array of HTML tags */
80: 'container' =>
'ul class=error',
85: 'container' =>
'fieldset',
87: 'description' =>
'p',
91: 'container' =>
'table',
96: '.required' =>
'required',
102: 'container' =>
'td',
106: 'description' =>
'small',
108: '.required' =>
'required',
110: '.password' =>
'text',
112: '.submit' =>
'button',
113: '.image' =>
'imagebutton',
114: '.button' =>
'button',
118: 'container' =>
'th',
123: 'container' =>
'div',
139: * Provides complete form rendering.
144: public function render(Form $form, $mode =
NULL)
152: if (!$mode ||
$mode ===
'begin') {
155: if ((!$mode &&
$this->getValue('form errors')) ||
$mode ===
'errors') {
158: if (!$mode ||
$mode ===
'body') {
161: if (!$mode ||
$mode ===
'end') {
170: * Sets JavaScript handler.
182: * Returns JavaScript handler.
202: if ($clientScript !==
NULL) {
203: $clientScript->enable();
206: // TODO: only for back compatiblity - remove?
208: foreach ($this->form->getControls() as $control) {
209: if ($control->getOption('required') &&
isset($wrapper['.required'])) {
210: $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
213: $el =
$control->getControlPrototype();
214: if ($el->getName() ===
'input' &&
isset($wrapper['.' .
$el->type])) {
215: $el->class($wrapper['.' .
$el->type], TRUE);
223: * Renders form begin.
230: foreach ($this->form->getControls() as $control) {
231: $control->setOption('rendered', FALSE);
246: foreach ($this->form->getControls() as $control) {
247: if ($control instanceof
HiddenField &&
!$control->getOption('rendered')) {
248: $s .= (string)
$control->getControl();
258: if ($clientScript !==
NULL) {
259: $s .=
$clientScript->renderClientScript() .
"\n";
268: * Renders validation errors (per form or per control).
269: * @param IFormControl
279: foreach ($errors as $error) {
284: $item->setText($error);
288: return "\n" .
$ul->render(0);
295: * Renders form body.
306: if (!$group->getControls() ||
!$group->getOption('visual')) continue;
308: $container =
$group->getOption('container', $defaultContainer);
309: $container =
$container instanceof
Html ?
clone $container :
Html::el($container);
311: $s .=
"\n" .
$container->startTag();
313: $text =
$group->getOption('label');
318: if ($translator !==
NULL) {
319: $text =
$translator->translate($text);
321: $s .=
"\n" .
$this->getWrapper('group label')->setText($text) .
"\n";
324: $text =
$group->getOption('description');
329: if ($translator !==
NULL) {
330: $text =
$translator->translate($text);
332: $s .=
$this->getWrapper('group description')->setText($text) .
"\n";
337: $remains =
$container->endTag() .
"\n" .
$remains;
338: if (!$group->getOption('embedNext')) {
347: $container->setHtml($s);
348: return $container->render(0);
354: * Renders group of controls.
355: * @param FormContainer|FormGroup
361: throw new InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
367: foreach ($parent->getControls() as $control) {
368: if ($control->getOption('rendered') ||
$control instanceof
HiddenField ||
$control->getForm(FALSE) !==
$this->form) {
372: $buttons[] =
$control;
389: $s .=
"\n" .
$container .
"\n";
398: * Renders single visual row.
399: * @param IFormControl
407: $pair->class($this->getValue($control->getOption('required') ?
'pair .required' :
'pair .optional'), TRUE);
408: $pair->class($control->getOption('class'), TRUE);
410: $pair->id =
$control->getOption('id');
411: return $pair->render(0);
417: * Renders single visual row of multiple controls.
418: * @param array of IFormControl
424: foreach ($controls as $control) {
426: throw new InvalidArgumentException("Argument must be array of IFormControl instances.");
428: $s[] = (string)
$control->getControl();
431: $pair->add($this->getWrapper('label container')->setHtml(' '));
432: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
439: * Renders 'label' part of visual row of controls.
440: * @param IFormControl
448: return $head->setHtml(' ');
451: return $head->setHtml((string)
$control->getLabel() .
$this->getValue('label suffix'));
458: * Renders 'control' part of visual row of controls.
459: * @param IFormControl
467: $description =
$control->getOption('description');
468: if ($description instanceof
Html) {
469: $description =
' ' .
$control->getOption('description');
472: $description =
' ' .
$this->getWrapper('control description')->setText($description);
483: return $body->setHtml((string)
$control->getControl() . (string)
$control->getLabel() .
$description);
486: return $body->setHtml((string)
$control->getControl() .
$description);