1. sfFormField.class.php
  2. /** * sfFormField represents a widget bind to a name and a value. * * @package symfony * @subpackage form * @author Fabien Potencier * @version SVN: $Id: sfFormField.class.php 22401 2009-09-25 03:49:27Z Kris.Wallsmith $ */
  3. class sfFormField
  4. {
  5. protected static
  6. $toStringException = null;
  7. protected
  8. $widget = null,
  9. $parent = null,
  10. $name = '',
  11. $value = null,
  12. $error = null;
  13. /**
  14. * Constructor.
  15. *
  16. * @param sfWidgetForm $widget A sfWidget instance
  17. * @param sfFormField $parent The sfFormField parent instance (null for the root widget)
  18. * @param string $name The field name
  19. * @param string $value The field value
  20. * @param sfValidatorError $error A sfValidatorError instance
  21. */
  22. public function __construct(sfWidgetForm $widget, sfFormField $parent = null, $name, $value, sfValidatorError $error = null)
  23. {
  24. $this->widget = $widget;
  25. $this->parent = $parent;
  26. $this->name = $name;
  27. $this->value = $value;
  28. $this->error = $error;
  29. }
  30. /**
  31. * Returns the string representation of this form field.
  32. *
  33. * @return string The rendered field
  34. */
  35. public function __toString()
  36. {
  37. try
  38. {
  39. return $this->render();
  40. }
  41. catch (Exception $e)
  42. {
  43. self::setToStringException($e);
  44. // we return a simple Exception message in case the form framework is used out of symfony.
  45. return 'Exception: '.$e->getMessage();
  46. }
  47. }
  48. /**
  49. * Returns true if a form thrown an exception in the __toString() method
  50. *
  51. * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
  52. *
  53. * @return boolean
  54. */
  55. static public function hasToStringException()
  56. {
  57. return null !== self::$toStringException;
  58. }
  59. /**
  60. * Gets the exception if one was thrown in the __toString() method.
  61. *
  62. * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
  63. *
  64. * @return Exception
  65. */
  66. static public function getToStringException()
  67. {
  68. return self::$toStringException;
  69. }
  70. /**
  71. * Sets an exception thrown by the __toString() method.
  72. *
  73. * This is a hack needed because PHP does not allow to throw exceptions in __toString() magic method.
  74. *
  75. * @param Exception $e The exception thrown by __toString()
  76. */
  77. static public function setToStringException(Exception $e)
  78. {
  79. if (null === self::$toStringException)
  80. {
  81. self::$toStringException = $e;
  82. }
  83. }
  84. /**
  85. * Renders the form field.
  86. *
  87. * @param array $attributes An array of HTML attributes
  88. *
  89. * @return string The rendered widget
  90. */
  91. function render($attributes = array())
  92. {
  93. if ($this->parent)
  94. {
  95. return $this->parent->getWidget()->renderField($this->name, $this->value, $attributes, $this->error);
  96. }
  97. else
  98. {
  99. return $this->widget->render($this->name, $this->value, $attributes, $this->error);
  100. }
  101. }
  102. /**
  103. * Returns a formatted row.
  104. *
  105. * The formatted row will use the parent widget schema formatter.
  106. * The formatted row contains the label, the field, the error and
  107. * the help message.
  108. *
  109. * @param array $attributes An array of HTML attributes to merge with the current attributes
  110. * @param string $label The label name (not null to override the current value)
  111. * @param string $help The help text (not null to override the current value)
  112. *
  113. * @return string The formatted row
  114. */
  115. public function renderRow($attributes = array(), $label = null, $help = null)
  116. {
  117. if (null === $this->parent)
  118. {
  119. throw new LogicException(sprintf('Unable to render the row for "%s".', $this->name));
  120. }
  121. $field = $this->parent->getWidget()->renderField($this->name, $this->value, !is_array($attributes) ? array() : $attributes, $this->error);
  122. $error = $this->error instanceof sfValidatorErrorSchema ? $this->error->getGlobalErrors() : $this->error;
  123. $help = null === $help ? $this->parent->getWidget()->getHelp($this->name) : $help;
  124. return strtr($this->parent->getWidget()->getFormFormatter()->formatRow($this->renderLabel($label), $field, $error, $help), array('%hidden_fields%' => ''));
  125. }
  126. /**
  127. * Returns a formatted error list.
  128. *
  129. * The formatted list will use the parent widget schema formatter.
  130. *
  131. * @return string The formatted error list
  132. */
  133. public function renderError()
  134. {
  135. if (null === $this->parent)
  136. {
  137. throw new LogicException(sprintf('Unable to render the error for "%s".', $this->name));
  138. }
  139. $error = $this->getWidget() instanceof sfWidgetFormSchema ? $this->getWidget()->getGlobalErrors($this->error) : $this->error;
  140. return $this->parent->getWidget()->getFormFormatter()->formatErrorsForRow($error);
  141. }
  142. /**
  143. * Returns the help text.
  144. *
  145. * @return string The help text
  146. */
  147. public function renderHelp()
  148. {
  149. if (null === $this->parent)
  150. {
  151. throw new LogicException(sprintf('Unable to render the help for "%s".', $this->name));
  152. }
  153. return $this->parent->getWidget()->getFormFormatter()->formatHelp($this->parent->getWidget()->getHelp($this->name));
  154. }
  155. /**
  156. * Returns the label tag.
  157. *
  158. * @param string $label The label name (not null to override the current value)
  159. * @param array $attributes Optional html attributes
  160. *
  161. * @return string The label tag
  162. */
  163. public function renderLabel($label = null, $attributes = array())
  164. {
  165. if (null === $this->parent)
  166. {
  167. throw new LogicException(sprintf('Unable to render the label for "%s".', $this->name));
  168. }
  169. if (null !== $label)
  170. {
  171. $currentLabel = $this->parent->getWidget()->getLabel($this->name);
  172. $this->parent->getWidget()->setLabel($this->name, $label);
  173. }
  174. $html = $this->parent->getWidget()->getFormFormatter()->generateLabel($this->name, $attributes);
  175. if (null !== $label)
  176. {
  177. $this->parent->getWidget()->setLabel($this->name, $currentLabel);
  178. }
  179. return $html;
  180. }
  181. /**
  182. * Returns the label name given a widget name.
  183. *
  184. * @return string The label name
  185. */
  186. public function renderLabelName()
  187. {
  188. if (null === $this->parent)
  189. {
  190. throw new LogicException(sprintf('Unable to render the label name for "%s".', $this->name));
  191. }
  192. return $this->parent->getWidget()->getFormFormatter()->generateLabelName($this->name);
  193. }
  194. /**
  195. * Returns the name attribute of the widget.
  196. *
  197. * @return string The name attribute of the widget
  198. */
  199. public function renderName()
  200. {
  201. return $this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name;
  202. }
  203. /**
  204. * Returns the id attribute of the widget.
  205. *
  206. * @return string The id attribute of the widget
  207. */
  208. public function renderId()
  209. {
  210. return $this->widget->generateId($this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name, $this->value);
  211. }
  212. /**
  213. * Returns true if the widget is hidden.
  214. *
  215. * @return Boolean true if the widget is hidden, false otherwise
  216. */
  217. public function isHidden()
  218. {
  219. return $this->widget->isHidden();
  220. }
  221. /**
  222. * Returns the widget name.
  223. *
  224. * @return mixed The widget name
  225. */
  226. public function getName()
  227. {
  228. return $this->name;
  229. }
  230. /**
  231. * Returns the widget value.
  232. *
  233. * @return mixed The widget value
  234. */
  235. public function getValue()
  236. {
  237. return $this->value;
  238. }
  239. /**
  240. * Returns the wrapped widget.
  241. *
  242. * @return sfWidget A sfWidget instance
  243. */
  244. public function getWidget()
  245. {
  246. return $this->widget;
  247. }
  248. /**
  249. * Returns the parent form field.
  250. *
  251. * @return sfFormField A sfFormField instance
  252. */
  253. public function getParent()
  254. {
  255. return $this->parent;
  256. }
  257. /**
  258. * Returns the error for this field.
  259. *
  260. * @return sfValidatorError A sfValidatorError instance
  261. */
  262. public function getError()
  263. {
  264. return $this->error;
  265. }
  266. /**
  267. * Returns true is the field has an error.
  268. *
  269. * @return Boolean true if the field has some errors, false otherwise
  270. */
  271. public function hasError()
  272. {
  273. return null !== $this->error && count($this->error);
  274. }
  275. }

Debug toolbar