Source for file Item.php
Documentation is available at Item.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 $
* Abstract superclass for Formagic items
* @author Florian Sonnenburg
* @copyright Copyright (c) 2007 Florian Sonnenburg
* @version $Id: Item.php 14 2007-08-13 20:33:26Z meweasle $
* Additional attributes for item
* Formagic object that called and contains item
* Array of rule object that are applied for this item
* Flag that defines if item content can be edited.
* Flag that defines if item will be displayed
* Flag that defines if item is removed from Formagic
* Flag that defines if item content will be posted on submit
* Pointer to submitted value for this item, stored in calling Formagic object
* @see Formagic_Item::setCaller()
* Values that are not changed by submitted values
* Pointer to default value for this item, stored in calling Formagic object
* @see FormagicItem::setCaller()
* Pointer to mulitpage value for this item, stored in calling Formagic object
* Flag that tells if this Formagic item has been submitted
private $_errorMessage = null;
* Defines if template takes full width
* @param string $type Type of item
* @param string $name Name of item
* @param array $args Additional attributes
* @param object $caller containing Formagic object
* @throws Formagic_Exception
public function __construct($type, $name, &$args= null, &$caller= null)
foreach($args as $key => $arg) {
"Calling Formagic object not found");
"Calling Formagic object not found");
"Calling Formagic object not found");
// Argument handler for unknown arguments. Defined in
* Handler for additional item arguments
* @param string $key Argument name
* @param mixed $arg Argument value
* Can be called to set caller object if not passed to constructor
if (isset ($this->_caller->submitValues[$this->name])) {
* Returns parent Formagic object
* Returns string representation of formItem object
$callerPresent = isset ($this->_caller);
$str = "FormagicItem [type: {$this->type}][name: { $this->name}]" .
* Returns current value for this item
* Calls Formagic::getValue() as all value storages are held there.
public function &getValue()
* Returns label for this item.
public function getLabel()
* HTML template for renderers that use HTML-Code
public function getHtml()
* Sets additional attributes for this item
* Mainly used for additional HTML attributes other than "name", "id" or
* "value", such as "style", "class", javascript-handlers etc. Attributes
* are added corresponding to key->value-pairs in $attArray.
public function setAttributes($attArray)
* Returns attribute string for HTML tag
protected function _getAttributeStr()
$res .= " $key=\"$att\"";
* Adds rule object to Formagic item
* Formagic items can have multiple rules, which are saved in
* First parameter $rule can either be a string or a FormagicRule object.
* String value is assumed to be the type of rule to be added.
* This method throws an exception if no valid role object can be identified.
* @param mixed $rule Rule type or FormagicRule object.
* @param string $errorMessage Message returned if rule is violated
* @param array $args Optional.
* @throws Formagic_Exception
public function addRule($rule, $errorMessage=null, $args=null)
// argument is assumed rule type if string.
$class = 'Formagic_Rule_' . ucFirst($rule);
$this->_rules[$rule] = new $class($rule,$errorMessage,$args);
} elseif($rule instanceof Formagic_Rule) {
$type = $rule->getType();
throw new Formagic_Exception('Invalid rule type or rule object');
* Tells if rule exists for this item
public function hasRule($rule)
return isset($this->_rules[$rule]);
* Iterates through all defined rules of Formagic item. Returns true if all
* rules apply or violated Formagic_Rule object
* @throws Formagic_Rule_Exception
* @return mixed Rule object or boolean
public function validate()
foreach($this->_rules as $ruleObj) {
if (!$ruleObj->check($this)) {
$this->_errorMessage = $ruleObj->getMessage();
* Returns item error message
* Returns error message if item check failed or empty string if not
public function getErrorMessage()
return $this->_errorMessage;
* Sets hidden flag for item
public function setHidden($flag)
* Returns hidden status of item
public function isHidden()
* Sets disabled flag for item and thus removes it from form
public function disable()
* Returns disabled status of item
public function isDisabled()
* @throws Formagic_Exception
public function _execEvent($event)
throw new Formagic_Exception('Event "' . $event . '" not supported');
* Executed before item validation
* Can be overwritten. Must return true if successful. If not,
* can either return false or throw exception
protected function _onPreValidate()
* Executed after item validation is done
protected function _onPostValidate()
* Executed when object is created
protected function _onCreate()
* Executed when Formagic::fetch() is called
protected function _onFetch()
|