Source for file ConventionalRenderer.php
Documentation is available at ConventionalRenderer.php
6: * @copyright Copyright (c) 2004, 2010 David Grudl
7: * @license http://nettephp.com/license Nette license
8: * @link http://nettephp.com
10: * @package Nette\Forms
16: * Converts a Form into the HTML output.
18: * @copyright Copyright (c) 2004, 2010 David Grudl
19: * @package Nette\Forms
24: * /--- form.container
26: * /--- if (form.errors) error.container
27: * .... error.item [.class]
30: * /--- hidden.container
31: * .... HIDDEN CONTROLS
34: * /--- group.container
36: * .... group.description
38: * /--- controls.container
40: * /--- pair.container [.required .optional .odd]
42: * /--- label.container
45: * .... label.requiredsuffix
48: * /--- control.container [.odd]
49: * .... CONTROL [.required .text .password .file .submit .button]
50: * .... control.requiredsuffix
51: * .... control.description
52: * .... if (control.errors) error.container
59: * @var array of HTML tags */
67: 'container' =>
'ul class=error',
72: 'container' =>
'fieldset',
74: 'description' =>
'p',
78: 'container' =>
'table',
83: '.required' =>
'required',
93: 'description' =>
'small',
94: 'requiredsuffix' =>
'',
96: '.required' =>
'required',
98: '.password' =>
'text',
100: '.submit' =>
'button',
101: '.image' =>
'imagebutton',
102: '.button' =>
'button',
106: 'container' =>
'th',
108: 'requiredsuffix' =>
'',
112: 'container' =>
'div',
128: * Provides complete form rendering.
133: public function render(Form $form, $mode =
NULL)
141: if (!$mode ||
$mode ===
'begin') {
144: if ((!$mode &&
$this->getValue('form errors')) ||
$mode ===
'errors') {
147: if (!$mode ||
$mode ===
'body') {
150: if (!$mode ||
$mode ===
'end') {
159: * Sets JavaScript handler.
161: * @return ConventionalRenderer provides a fluent interface
172: * Returns JavaScript handler.
192: if ($clientScript !==
NULL) {
193: $clientScript->enable();
196: // TODO: only for back compatiblity - remove?
198: foreach ($this->form->getControls() as $control) {
199: if ($control->getOption('required') &&
isset($wrapper['.required'])) {
200: $control->getLabelPrototype()->class($wrapper['.required'], TRUE);
203: $el =
$control->getControlPrototype();
204: if ($el->getName() ===
'input' &&
isset($wrapper['.' .
$el->type])) {
205: $el->class($wrapper['.' .
$el->type], TRUE);
213: * Renders form begin.
220: foreach ($this->form->getControls() as $control) {
221: $control->setOption('rendered', FALSE);
227: $el->action =
$uri[0];
229: if (isset($uri[1])) {
234: $s .=
Html::el('input', array('type' =>
'hidden', 'name' =>
$name, 'value' =>
urldecode($parts[1])));
237: $s =
"\n\t" .
$this->getWrapper('hidden container')->setHtml($s);
239: return $el->startTag() .
$s;
256: foreach ($this->form->getControls() as $control) {
257: if ($control instanceof
HiddenField &&
!$control->getOption('rendered')) {
258: $s .= (string)
$control->getControl();
268: if ($clientScript !==
NULL) {
269: $s .=
$clientScript->renderClientScript() .
"\n";
278: * Renders validation errors (per form or per control).
279: * @param IFormControl
289: foreach ($errors as $error) {
294: $item->setText($error);
298: return "\n" .
$ul->render(0);
305: * Renders form body.
316: if (!$group->getControls() ||
!$group->getOption('visual')) continue;
318: $container =
$group->getOption('container', $defaultContainer);
319: $container =
$container instanceof
Html ?
clone $container :
Html::el($container);
321: $s .=
"\n" .
$container->startTag();
323: $text =
$group->getOption('label');
328: if ($translator !==
NULL) {
329: $text =
$translator->translate($text);
331: $s .=
"\n" .
$this->getWrapper('group label')->setText($text) .
"\n";
334: $text =
$group->getOption('description');
339: if ($translator !==
NULL) {
340: $text =
$translator->translate($text);
342: $s .=
$this->getWrapper('group description')->setText($text) .
"\n";
347: $remains =
$container->endTag() .
"\n" .
$remains;
348: if (!$group->getOption('embedNext')) {
357: $container->setHtml($s);
358: return $container->render(0);
364: * Renders group of controls.
365: * @param FormContainer|FormGroup
371: throw new InvalidArgumentException("Argument must be FormContainer or FormGroup instance.");
377: foreach ($parent->getControls() as $control) {
378: if ($control->getOption('rendered') ||
$control instanceof
HiddenField ||
$control->getForm(FALSE) !==
$this->form) {
382: $buttons[] =
$control;
399: $s .=
"\n" .
$container .
"\n";
408: * Renders single visual row.
409: * @param IFormControl
417: $pair->class($this->getValue($control->getOption('required') ?
'pair .required' :
'pair .optional'), TRUE);
418: $pair->class($control->getOption('class'), TRUE);
420: $pair->id =
$control->getOption('id');
421: return $pair->render(0);
427: * Renders single visual row of multiple controls.
428: * @param array of IFormControl
434: foreach ($controls as $control) {
436: throw new InvalidArgumentException("Argument must be array of IFormControl instances.");
438: $s[] = (string)
$control->getControl();
442: $pair->add($this->getWrapper('control container')->setHtml(implode(" ", $s)));
449: * Renders 'label' part of visual row of controls.
450: * @param IFormControl
458: return $head->setHtml(($head->getName() ===
'td' ||
$head->getName() ===
'th') ?
' ' :
'');
461: $label =
$control->getLabel();
462: $suffix =
$this->getValue('label suffix') .
($control->getOption('required') ?
$this->getValue('label requiredsuffix') :
'');
464: $label->setHtml($label->getHtml() .
$suffix);
467: return $head->setHtml((string)
$label .
$suffix);
474: * Renders 'control' part of visual row of controls.
475: * @param IFormControl
483: $description =
$control->getOption('description');
484: if ($description instanceof
Html) {
485: $description =
' ' .
$control->getOption('description');
488: $description =
' ' .
$this->getWrapper('control description')->setText($control->translate($description));
494: if ($control->getOption('required')) {
495: $description =
$this->getValue('control requiredsuffix') .
$description;
503: return $body->setHtml((string)
$control->getControl() . (string)
$control->getLabel() .
$description);
506: return $body->setHtml((string)
$control->getControl() .
$description);