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

Debug toolbar