| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class sfFormFieldSchema extends sfFormField implements ArrayAccess, Iterator, Countable |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$count = 0, |
|---|
| 23 |
$fieldNames = array(), |
|---|
| 24 |
$fields = array(); |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
* Constructor. |
|---|
| 28 |
* |
|---|
| 29 |
* @param sfWidgetFormSchema $widget A sfWidget instance |
|---|
| 30 |
* @param sfFormField $parent The sfFormField parent instance (null for the root widget) |
|---|
| 31 |
* @param string $name The field name |
|---|
| 32 |
* @param string $value The field value |
|---|
| 33 |
* @param sfValidatorError $error A sfValidatorError instance |
|---|
| 34 |
*/ |
|---|
| 35 |
public function __construct(sfWidgetFormSchema $widget, sfFormField $parent = null, $name, $value, sfValidatorError $error = null) |
|---|
| 36 |
{ |
|---|
| 37 |
parent::__construct($widget, $parent, $name, $value, $error); |
|---|
| 38 |
|
|---|
| 39 |
$this->fieldNames = $widget->getPositions(); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
* Renders hidden form fields. |
|---|
| 44 |
* |
|---|
| 45 |
* @param boolean $recursive False will prevent hidden fields from embedded forms from rendering |
|---|
| 46 |
* |
|---|
| 47 |
* @return string |
|---|
| 48 |
*/ |
|---|
| 49 |
public function renderHiddenFields($recursive = true) |
|---|
| 50 |
{ |
|---|
| 51 |
$output = ''; |
|---|
| 52 |
|
|---|
| 53 |
foreach ($this->getHiddenFields($recursive) as $field) |
|---|
| 54 |
{ |
|---|
| 55 |
$output .= $field->render(); |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
return $output; |
|---|
| 59 |
} |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
* Returns an array of hidden fields from the current schema. |
|---|
| 63 |
* |
|---|
| 64 |
* @param boolean $recursive Whether to recur through embedded schemas |
|---|
| 65 |
* |
|---|
| 66 |
* @return array |
|---|
| 67 |
*/ |
|---|
| 68 |
public function getHiddenFields($recursive = true) |
|---|
| 69 |
{ |
|---|
| 70 |
$fields = array(); |
|---|
| 71 |
|
|---|
| 72 |
foreach ($this as $name => $field) |
|---|
| 73 |
{ |
|---|
| 74 |
if ($field instanceof sfFormFieldSchema && $recursive) |
|---|
| 75 |
{ |
|---|
| 76 |
$fields = array_merge($fields, $field->getHiddenFields($recursive)); |
|---|
| 77 |
} |
|---|
| 78 |
else if ($field->isHidden()) |
|---|
| 79 |
{ |
|---|
| 80 |
$fields[] = $field; |
|---|
| 81 |
} |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
return $fields; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
* Returns true if the bound field exists (implements the ArrayAccess interface). |
|---|
| 89 |
* |
|---|
| 90 |
* @param string $name The name of the bound field |
|---|
| 91 |
* |
|---|
| 92 |
* @return Boolean true if the widget exists, false otherwise |
|---|
| 93 |
*/ |
|---|
| 94 |
public function offsetExists($name) |
|---|
| 95 |
{ |
|---|
| 96 |
return isset($this->widget[$name]); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
* Returns the form field associated with the name (implements the ArrayAccess interface). |
|---|
| 101 |
* |
|---|
| 102 |
* @param string $name The offset of the value to get |
|---|
| 103 |
* |
|---|
| 104 |
* @return sfFormField A form field instance |
|---|
| 105 |
*/ |
|---|
| 106 |
public function offsetGet($name) |
|---|
| 107 |
{ |
|---|
| 108 |
if (!isset($this->fields[$name])) |
|---|
| 109 |
{ |
|---|
| 110 |
if (null === $widget = $this->widget[$name]) |
|---|
| 111 |
{ |
|---|
| 112 |
throw new InvalidArgumentException(sprintf('Widget "%s" does not exist.', $name)); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
$error = isset($this->error[$name]) ? $this->error[$name] : null; |
|---|
| 116 |
|
|---|
| 117 |
if ($widget instanceof sfWidgetFormSchema) |
|---|
| 118 |
{ |
|---|
| 119 |
$class = 'sfFormFieldSchema'; |
|---|
| 120 |
|
|---|
| 121 |
if ($error && !$error instanceof sfValidatorErrorSchema) |
|---|
| 122 |
{ |
|---|
| 123 |
$error = new sfValidatorErrorSchema($error->getValidator(), array($error)); |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
else |
|---|
| 127 |
{ |
|---|
| 128 |
$class = 'sfFormField'; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
$this->fields[$name] = new $class($widget, $this, $name, isset($this->value[$name]) ? $this->value[$name] : null, $error); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
return $this->fields[$name]; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
* Throws an exception saying that values cannot be set (implements the ArrayAccess interface). |
|---|
| 139 |
* |
|---|
| 140 |
* @param string $offset (ignored) |
|---|
| 141 |
* @param string $value (ignored) |
|---|
| 142 |
* |
|---|
| 143 |
* @throws LogicException |
|---|
| 144 |
*/ |
|---|
| 145 |
public function offsetSet($offset, $value) |
|---|
| 146 |
{ |
|---|
| 147 |
throw new LogicException('Cannot update form fields (read-only).'); |
|---|
| 148 |
} |
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
* Throws an exception saying that values cannot be unset (implements the ArrayAccess interface). |
|---|
| 152 |
* |
|---|
| 153 |
* @param string $offset (ignored) |
|---|
| 154 |
* |
|---|
| 155 |
* @throws LogicException |
|---|
| 156 |
*/ |
|---|
| 157 |
public function offsetUnset($offset) |
|---|
| 158 |
{ |
|---|
| 159 |
throw new LogicException('Cannot remove form fields (read-only).'); |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
* Resets the field names array to the beginning (implements the Iterator interface). |
|---|
| 164 |
*/ |
|---|
| 165 |
public function rewind() |
|---|
| 166 |
{ |
|---|
| 167 |
reset($this->fieldNames); |
|---|
| 168 |
$this->count = count($this->fieldNames); |
|---|
| 169 |
} |
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
* Gets the key associated with the current form field (implements the Iterator interface). |
|---|
| 173 |
* |
|---|
| 174 |
* @return string The key |
|---|
| 175 |
*/ |
|---|
| 176 |
public function key() |
|---|
| 177 |
{ |
|---|
| 178 |
return current($this->fieldNames); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
* Returns the current form field (implements the Iterator interface). |
|---|
| 183 |
* |
|---|
| 184 |
* @return mixed The escaped value |
|---|
| 185 |
*/ |
|---|
| 186 |
public function current() |
|---|
| 187 |
{ |
|---|
| 188 |
return $this[current($this->fieldNames)]; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 |
* Moves to the next form field (implements the Iterator interface). |
|---|
| 193 |
*/ |
|---|
| 194 |
public function next() |
|---|
| 195 |
{ |
|---|
| 196 |
next($this->fieldNames); |
|---|
| 197 |
--$this->count; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
* Returns true if the current form field is valid (implements the Iterator interface). |
|---|
| 202 |
* |
|---|
| 203 |
* @return boolean The validity of the current element; true if it is valid |
|---|
| 204 |
*/ |
|---|
| 205 |
public function valid() |
|---|
| 206 |
{ |
|---|
| 207 |
return $this->count > 0; |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
|
|---|
| 211 |
* Returns the number of form fields (implements the Countable interface). |
|---|
| 212 |
* |
|---|
| 213 |
* @return integer The number of embedded form fields |
|---|
| 214 |
*/ |
|---|
| 215 |
public function count() |
|---|
| 216 |
{ |
|---|
| 217 |
return count($this->fieldNames); |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|