| [ Index ] |
PHP Cross Reference of Limb3 |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 * Limb PHP Framework 4 * 5 * @link http://limb-project.com 6 * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com) 7 * @license LGPL http://www.gnu.org/copyleft/lesser.html 8 */ 9 10 /** 11 * class WactFormComponent. 12 * 13 * @package wact 14 * @version $Id: WactFormComponent.class.php 5945 2007-06-06 08:31:43Z pachanga $ 15 */ 16 class WactFormComponent extends WactRuntimeTagComponent 17 { 18 protected $error_list; 19 20 protected $is_valid = TRUE; 21 22 protected $state_vars = array(); 23 24 protected $_datasource; 25 26 protected function _ensureDataSourceAvailable() 27 { 28 if (!isset($this->_datasource)) 29 $this->registerDataSource(new ArrayObject()); 30 } 31 32 function get($name) 33 { 34 $this->_ensureDataSourceAvailable(); 35 return $this->_datasource->get($name); 36 } 37 38 function set($name, $value) 39 { 40 $this->_ensureDataSourceAvailable(); 41 $this->_datasource->set($name, $value); 42 } 43 44 /** 45 * Get the named property from the form DataSource 46 * @param string variable name 47 * @return mixed value or void if not found 48 * @access public 49 * @deprecated will probablybe removed in a future reorganization of 50 * how form elements become associated with their values 51 */ 52 function getValue($name) 53 { 54 $this->_ensureDataSourceAvailable(); 55 return $this->_datasource->get($name); 56 } 57 58 /** 59 * Set a named property in the form DataSource 60 */ 61 function setValue($name, $value) 62 { 63 $this->_ensureDataSourceAvailable(); 64 $this->_datasource->set($name, $value); 65 } 66 67 function prepare() 68 { 69 $this->_ensureDataSourceAvailable(); 70 } 71 72 function registerDataSource($datasource) 73 { 74 $this->_datasource = WactTemplate :: makeObject($datasource); 75 } 76 77 function getDataSource() 78 { 79 return $this->_datasource; 80 } 81 82 /** 83 * Finds the WactLabelComponent associated with a form field, allowing 84 * an error message to be displayed next to the field. Called by this 85 * setErrors. 86 */ 87 function findLabel($field_id, $component) 88 { 89 foreach( array_keys($component->children) as $key) 90 { 91 $child = $component->children[$key]; 92 if (is_a($child, 'WactLabelComponent') && $child->getAttribute('for') == $field_id) 93 return $child; 94 elseif ($result = $this->findLabel($field_id, $child)) 95 return $result; 96 } 97 } 98 99 /** 100 * If errors occur, use this method to identify them to the FormComponent. 101 * (typically this is called for you by controllers) 102 * @param ErrorList 103 */ 104 function setErrors($ErrorList) 105 { 106 foreach($ErrorList as $Error) 107 { 108 $this->is_valid = FALSE; 109 110 // Find the component(s) that the error applies to and tell 111 // them there was an error (using their setError() method) 112 // as well as notifying related label components if found 113 foreach ($Error->getFields() as $fieldName) 114 { 115 $Field = $this->findChild($fieldName); 116 if (is_object($Field)) 117 { 118 $Field->setError(); 119 if ($Field->hasAttribute('id')) 120 { 121 $Label = $this->findLabel($Field->getAttribute('id'), $this); 122 if ($Label) 123 $Label->setError(); 124 } 125 } 126 } 127 } 128 129 $this->error_list = $ErrorList; 130 } 131 132 function hasErrors() 133 { 134 return !$this->is_valid; 135 } 136 137 /** 138 * Returns the ErrorList if it exists or an EmptyErrorList if not 139 * (typically this is called for you by controllers) 140 * @return object WactErrorList or EmptyArrayIterator 141 */ 142 function getErrorsDataSet() 143 { 144 if (!isset($this->error_list)) 145 return new ArrayIterator(array()); 146 147 return $this->error_list; 148 } 149 150 function getFieldErrorsDataset($for = '') 151 { 152 if (!isset($this->error_list)) 153 return new ArrayIterator(array()); 154 155 $result = array(); 156 foreach($this->error_list as $error) 157 { 158 if(!$for) 159 $this->_appendErrorsForEachField($result, $error); 160 else 161 $this->_appendErrorForField($result, $error, $for); 162 } 163 164 return new ArrayIterator($result); 165 } 166 167 protected function _appendErrorsForEachField(&$result, $error) 168 { 169 $fields = $error->getFields(); 170 foreach($fields as $alias => $field) 171 { 172 $field_error = clone($error); 173 $field_error['id'] = $field; 174 $result[] = $field_error; 175 } 176 } 177 178 protected function _appendErrorForField(&$result, $error, $field) 179 { 180 if(!in_array($field, $error->getFields())) 181 return; 182 183 $field_error = clone($error); 184 $field_error['id'] = $field; 185 $result[] = $field_error; 186 } 187 188 /** 189 * Identify a property stored in the DataSource of the component, which 190 * should be passed as a hidden input field in the form post. The name 191 * attribute of the hidden input field will be the name of the property. 192 * Use this to have properties persist between form submits 193 * @see renderState() 194 */ 195 function preserveState($variable) 196 { 197 $this->state_vars[] = $variable; 198 } 199 200 /** 201 * Renders the hidden fields for variables which should be preserved. 202 * Called from within a compiled template render function. 203 */ 204 function renderState() 205 { 206 foreach ($this->state_vars as $var) 207 { 208 echo '<input type="hidden" name="'; 209 echo $var; 210 echo '" value="'; 211 echo htmlspecialchars($this->getValue($var), ENT_QUOTES); 212 echo '"/>'; 213 } 214 } 215 } 216 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Dec 1 03:56:46 2008 | Cross-referenced by PHPXref 0.7 |