Source for file HTML.php
Documentation is available at HTML.php
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at
* http://formagic.weasle.de/licence.txt
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to licence@weasle.de so we can send you a copy immediately.
* @author Florian Sonnenburg
* @copyright Copyright (c) 2007 Florian Sonnenburg
* @license http://formagic.weasle.de/licence.txt New BSD License
* @revision $Revision: 14 $
* @author Florian Sonnenburg
* @copyright Copyright (c) 2007
* @version $Id: HTML.php 14 2007-08-13 20:33:26Z meweasle $
* Form action, value of <form action=""> tag
* Overwrites default templates for item types or individual items
private $_templates = array();
private $_hiddenString = "";
* Error/notice/debug colors
Formagic::ERROR => '#F41414',
Formagic::NOTICE => '#DCB913',
Formagic::DEBUG => '#E2D726');
* HTML string for mandatory fields
// $this->_action = $_SERVER['PHP_SELF'];
<!-- formagic HTML renderer start
============================= -->
<form name="%NAME%" action="%ACTION%" id="%NAME%" method="%METHOD%">
<!-- hidden inputs -->%HIDDENS%<!-- hidden end -->
<!-- formagic HTML renderer end
=========================== -->
// set container main template
$this->setTemplate('<table border="0" cellpadding="4" cellspacing="0"%ATTRIBUTES%>
'main', null, 'container');
<!-- Input -->%INPUT%</td>
'item', null, 'container');
<!-- Input -->%INPUT%</td>
'itemWide', null, 'container');
$this->setTemplate('<!-- Label --><b><label for="%ID%"%ERRORCLASS%>%LABEL%%ERRORMARKER%</label></b>',
'label', null, 'container');
$this->setTemplate('<!-- Error --><span style="color:#ff0000">%ERRORMSG%</span><br />',
'error', null, 'container');
* Formagic_Renderer_HTML::setCustomTemplate()
* @param string $template
* @param string $itemType
public function setTemplate($template, $part= 'main', $item= null, $itemType= null)
$this->_templates['__fm'][$item][$part] = $template;
$this->_templates[$itemType][$part] = $template;
$this->_templates['root'] = $template;
* Returns surrounding template
return $this->_templates['root'];
* Returns template string
* @param string $itemType
private function _getTemplate($part= 'main', $item= null, $itemType= null)
if ($item && isset ($this->_templates['__fm'][$item][$part])) {
return $this->_templates['__fm'][$item][$part];
// don't check class hierarchy if not necessary
if (isset ($this->_templates[$itemType][$part])) {
return $this->_templates[$itemType][$part];
foreach($parents as $parent) {
$itemTypeParent = strToLower(str_replace('Formagic_Item_', '', $parent));
if (isset ($this->_templates[$itemTypeParent][$part])) {
$this->_templates[$itemType] = & $this->_templates[$itemTypeParent];
return $this->_templates[$itemType][$part];
* Returns form HTML string
public function fetch(&$fObj)
$content = $this->_renderSubItemsRecursive($fObj->getItemHolder());
$messages = $fObj->getMessages();
foreach($messages as $level => $texts) {
foreach($texts as $msg) {
$msgStr .= '<li style="color:' . $this->messageColors[$level] . '">';
$msgStr .= $msg . '</li>';
$str = str_replace(array('%MESSAGE%', '%NAME%', '%ACTION%', '%METHOD%', '%HIDDENS%', '%CONTENT%'),
array($msgStr, $fObj->name, $fObj->getFormAction(), $fObj->method, $this->_hiddenString, $content),
* Adds hidden fields to form HTML string
private function _saveHiddenItem($name, $value)
$this->_hiddenString .= "<input type=\"hidden\" name=\"$name\" value=\"$value\" />\n";
* Recursively iterate through item objects
private function _renderSubItemsRecursive($container, $start = false)
$items = $container->getItems();
$containerTpl = $this->_getTemplate('main', $container->name, $container->type);
$errorTpl = $this->_getTemplate('error', $container->name, $container->type);
$labelTpl = $this->_getTemplate('label', $container->name, $container->type);
$itemTpl = $this->_getTemplate('item', $container->name, $container->type);
$itemWideTpl = $this->_getTemplate('itemWide', $container->name, $container->type);
foreach($items as &$item) {
if ($item->isDisabled()) {
* Run through containers recursively
// render fake input string from container items
$rows .= $this->_renderSubItemsRecursive($item);
* Catch hiddens and continue to next input
$this->_saveHiddenItem($item->name, $item->getValue());
// template spans both label and input column
// separate label and input columns
$msg = $item->getErrorMessage();
// or discard it if no error message present
$itemLabel = $item->getLabel();
if ($item->hasRule('mandatory')) {
$errorClass = ' class="inputError"';
array('%LABEL%', '%ERRORCLASS%', '%ID%', '%ERRORMARKER%'),
array($itemLabel, $errorClass, $item->id, $errorMarker),
// display nothing if no label present
* render input HTML string
array('%LABEL%', '%ERROR%', '%INPUT%'),
array($label, $error, $item->getHtml()),
$res = $container->hasHtml
array('%ROWS%', '%ATTRIBUTES%'),
array($rows, $container->getAttributeStr()), $containerTpl)
* Reformats $string so that it can be used as HTML ID
private function _htmlId($string)
return str_replace(array('[',']'), array('_',''), $string);
|