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.
185: * Returns JavaScript handler.
205: if ($clientScript !==
NULL) {
206: $clientScript->enable();
209: // TODO: only for back compatiblity - remove?
211: foreach ($this->form->getControls() as $control) {
212: if ($control->getOption('required') &&
isset($wrapper['.required'])) {
213: $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
216: $el =
$control->getControlPrototype();
217: if ($el->getName() ===
'input' &&
isset($wrapper['.' .
$el->type])) {
218: $el->class($wrapper['.' .
$el->type], TRUE);
226: * Renders form begin.
233: foreach ($this->form->getControls() as $control) {
234: $control->setOption('rendered', FALSE);
240: $el->action =
$uri[0];
242: if (isset($uri[1])) {
247: $s =
"\n\t" .
$this->getWrapper('hidden container')->setHtml($s);
249: return $el->startTag() .
$s;
266: foreach ($this->form->getControls() as $control) {
267: if ($control instanceof
HiddenField &&
!$control->getOption('rendered')) {
268: $s .= (string)
$control->getControl();
278: if ($clientScript !==
NULL) {
279: $s .=
$clientScript->renderClientScript() .
"\n";
288: * Renders validation errors (per form or per control).
289: * @param IFormControl
299: foreach ($errors as $error) {
304: $item->setText($error);
308: return "\n" .
$ul->render(0);
315: * Renders form body.
326: if (!$group->getControls() ||
!$group->getOption('visual')) continue;
328: $container =
$group->getOption('container', $defaultContainer);
329: $container =
$container instanceof
Html ?
clone $container :
Html::el($container);
331: $s .=
"\n" .
$container->startTag();
333: $text =
$group->getOption('label');
338: if ($translator !==
NULL) {
339: $text =
$translator->translate($text);
341: $s .=
"\n" .
$this->getWrapper('group label')->setText($text) .
"\n";
344: $text =
$group->getOption('description');
349: if ($translator !==
NULL) {
350: $text =
$translator->translate($text);
352: $s .=
$this->getWrapper('group description')->setText($text) .
"\n";
357: $remains =
$container->endTag() .
"\n" .
$remains;
358: if (!$group->getOption('embedNext')) {
367: $container->setHtml($s);
368: return $container->render(0);
374: * Renders group of controls.
375: * @param FormContainer|FormGroup
381: throw new InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
387: foreach ($parent->getControls() as $control) {
388: if ($control->getOption('rendered') ||
$control instanceof
HiddenField ||
$control->getForm(FALSE) !==
$this->form) {
392: $buttons[] =
$control;
409: $s .=
"\n" .
$container .
"\n";
418: * Renders single visual row.
419: * @param IFormControl
427: $pair->class($this->getValue($control->getOption('required') ?
'pair .required' :
'pair .optional'), TRUE);
428: $pair->class($control->getOption('class'), TRUE);
430: $pair->id =
$control->getOption('id');
431: return $pair->render(0);
437: * Renders single visual row of multiple controls.
438: * @param array of IFormControl
444: foreach ($controls as $control) {
446: throw new InvalidArgumentException("Argument must be array of IFormControl instances.");
448: $s[] = (string)
$control->getControl();
452: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
459: * Renders 'label' part of visual row of controls.
460: * @param IFormControl
468: return $head->setHtml(($head->getName() ===
'td' ||
$head->getName() ===
'th') ?
' ' :
'');
471: $label =
$control->getLabel();
472: $suffix =
$this->getValue('label suffix') .
($control->getOption('required') ?
$this->getValue('label requiredsuffix') :
'');
474: $label->setHtml($label->getHtml() .
$suffix);
477: return $head->setHtml((string)
$label .
$suffix);
484: * Renders 'control' part of visual row of controls.
485: * @param IFormControl
493: $description =
$control->getOption('description');
494: if ($description instanceof
Html) {
495: $description =
' ' .
$control->getOption('description');
498: $description =
' ' .
$this->getWrapper('control description')->setText($description);
504: if ($control->getOption('required')) {
505: $description =
$this->getValue('control requiredsuffix') .
$description;
513: return $body->setHtml((string)
$control->getControl() . (string)
$control->getLabel() .
$description);
516: return $body->setHtml((string)
$control->getControl() .
$description);