sfFormField.class.php 7.2 KB

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