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 http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
17: * @package Nette\Forms
22: require_once dirname(__FILE__) .
'/../../Object.php';
24: require_once dirname(__FILE__) .
'/../../Forms/IFormRenderer.php';
29: * Converts a Form into the HTML output.
31: * @author David Grudl
32: * @copyright Copyright (c) 2004, 2009 David Grudl
33: * @package Nette\Forms
38: * /--- form.container
40: * /--- if (form.errors) error.container
41: * .... error.item [.class]
44: * /--- hidden.container
45: * .... HIDDEN CONTROLS
48: * /--- group.container
50: * .... group.description
52: * /--- controls.container
54: * /--- pair.container [.required .optional .odd]
56: * /--- label.container
59: * .... label.requiredsuffix
62: * /--- control.container [.odd]
63: * .... CONTROL [.required .text .password .file .submit .button]
64: * .... control.requiredsuffix
65: * .... control.description
66: * .... if (control.errors) error.container
73: * @var array of HTML tags */
81: 'container' =>
'ul class=error',
86: 'container' =>
'fieldset',
88: 'description' =>
'p',
92: 'container' =>
'table',
97: '.required' =>
'required',
103: 'container' =>
'td',
107: 'description' =>
'small',
108: 'requiredsuffix' =>
'',
110: '.required' =>
'required',
112: '.password' =>
'text',
114: '.submit' =>
'button',
115: '.image' =>
'imagebutton',
116: '.button' =>
'button',
120: 'container' =>
'th',
122: 'requiredsuffix' =>
'',
126: 'container' =>
'div',
142: * Provides complete form rendering.
147: public function render(Form $form, $mode =
NULL)
155: if (!$mode ||
$mode ===
'begin') {
158: if ((!$mode &&
$this->getValue('form errors')) ||
$mode ===
'errors') {
161: if (!$mode ||
$mode ===
'body') {
164: if (!$mode ||
$mode ===
'end') {
173: * Sets JavaScript handler.
175: * @return ConventionalRenderer provides a fluent interface
186: * Returns JavaScript handler.
206: if ($clientScript !==
NULL) {
207: $clientScript->enable();
210: // TODO: only for back compatiblity - remove?
212: foreach ($this->form->getControls() as $control) {
213: if ($control->getOption('required') &&
isset($wrapper['.required'])) {
214: $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
217: $el =
$control->getControlPrototype();
218: if ($el->getName() ===
'input' &&
isset($wrapper['.' .
$el->type])) {
219: $el->class($wrapper['.' .
$el->type], TRUE);
227: * Renders form begin.
234: foreach ($this->form->getControls() as $control) {
235: $control->setOption('rendered', FALSE);
241: $el->action =
$uri[0];
243: if (isset($uri[1])) {
248: $s .=
Html::el('input', array('type' =>
'hidden', 'name' =>
$name, 'value' =>
urldecode($parts[1])));
251: $s =
"\n\t" .
$this->getWrapper('hidden container')->setHtml($s);
253: return $el->startTag() .
$s;
270: foreach ($this->form->getControls() as $control) {
271: if ($control instanceof
HiddenField &&
!$control->getOption('rendered')) {
272: $s .= (string)
$control->getControl();
282: if ($clientScript !==
NULL) {
283: $s .=
$clientScript->renderClientScript() .
"\n";
292: * Renders validation errors (per form or per control).
293: * @param IFormControl
303: foreach ($errors as $error) {
308: $item->setText($error);
312: return "\n" .
$ul->render(0);
319: * Renders form body.
330: if (!$group->getControls() ||
!$group->getOption('visual')) continue;
332: $container =
$group->getOption('container', $defaultContainer);
333: $container =
$container instanceof
Html ?
clone $container :
Html::el($container);
335: $s .=
"\n" .
$container->startTag();
337: $text =
$group->getOption('label');
342: if ($translator !==
NULL) {
343: $text =
$translator->translate($text);
345: $s .=
"\n" .
$this->getWrapper('group label')->setText($text) .
"\n";
348: $text =
$group->getOption('description');
353: if ($translator !==
NULL) {
354: $text =
$translator->translate($text);
356: $s .=
$this->getWrapper('group description')->setText($text) .
"\n";
361: $remains =
$container->endTag() .
"\n" .
$remains;
362: if (!$group->getOption('embedNext')) {
371: $container->setHtml($s);
372: return $container->render(0);
378: * Renders group of controls.
379: * @param FormContainer|FormGroup
385: throw new InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
391: foreach ($parent->getControls() as $control) {
392: if ($control->getOption('rendered') ||
$control instanceof
HiddenField ||
$control->getForm(FALSE) !==
$this->form) {
396: $buttons[] =
$control;
413: $s .=
"\n" .
$container .
"\n";
422: * Renders single visual row.
423: * @param IFormControl
431: $pair->class($this->getValue($control->getOption('required') ?
'pair .required' :
'pair .optional'), TRUE);
432: $pair->class($control->getOption('class'), TRUE);
434: $pair->id =
$control->getOption('id');
435: return $pair->render(0);
441: * Renders single visual row of multiple controls.
442: * @param array of IFormControl
448: foreach ($controls as $control) {
450: throw new InvalidArgumentException("Argument must be array of IFormControl instances.");
452: $s[] = (string)
$control->getControl();
456: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
463: * Renders 'label' part of visual row of controls.
464: * @param IFormControl
472: return $head->setHtml(($head->getName() ===
'td' ||
$head->getName() ===
'th') ?
' ' :
'');
475: $label =
$control->getLabel();
476: $suffix =
$this->getValue('label suffix') .
($control->getOption('required') ?
$this->getValue('label requiredsuffix') :
'');
478: $label->setHtml($label->getHtml() .
$suffix);
481: return $head->setHtml((string)
$label .
$suffix);
488: * Renders 'control' part of visual row of controls.
489: * @param IFormControl
497: $description =
$control->getOption('description');
498: if ($description instanceof
Html) {
499: $description =
' ' .
$control->getOption('description');
502: $description =
' ' .
$this->getWrapper('control description')->setText($description);
508: if ($control->getOption('required')) {
509: $description =
$this->getValue('control requiredsuffix') .
$description;
517: return $body->setHtml((string)
$control->getControl() . (string)
$control->getLabel() .
$description);
520: return $body->setHtml((string)
$control->getControl() .
$description);