sfFormField.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 15807 2009-02-26 10:45:51Z fabien $
  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 help text.
  151. *
  152. * @return string The help text
  153. */
  154. public function renderHelp()
  155. {
  156. if (is_null($this->parent))
  157. {
  158. throw new LogicException(sprintf('Unable to render the help for "%s".', $this->name));
  159. }
  160. return $this->parent->getWidget()->getFormFormatter()->formatHelp($this->parent->getWidget()->getHelp($this->name));
  161. }
  162. /**
  163. * Returns the label tag.
  164. *
  165. * @param string $label The label name (not null to override the current value)
  166. * @param array $attributes Optional html attributes
  167. *
  168. * @return string The label tag
  169. */
  170. public function renderLabel($label = null, $attributes = array())
  171. {
  172. if (is_null($this->parent))
  173. {
  174. throw new LogicException(sprintf('Unable to render the label for "%s".', $this->name));
  175. }
  176. if (!is_null($label))
  177. {
  178. $currentLabel = $this->parent->getWidget()->getLabel($this->name);
  179. $this->parent->getWidget()->setLabel($this->name, $label);
  180. }
  181. $html = $this->parent->getWidget()->getFormFormatter()->generateLabel($this->name, $attributes);
  182. if (!is_null($label))
  183. {
  184. $this->parent->getWidget()->setLabel($this->name, $currentLabel);
  185. }
  186. return $html;
  187. }
  188. /**
  189. * Returns the label name given a widget name.
  190. *
  191. * @return string The label name
  192. */
  193. public function renderLabelName()
  194. {
  195. if (is_null($this->parent))
  196. {
  197. throw new LogicException(sprintf('Unable to render the label name for "%s".', $this->name));
  198. }
  199. return $this->parent->getWidget()->getFormFormatter()->generateLabelName($this->name);
  200. }
  201. /**
  202. * Returns the id attribute of the widget.
  203. *
  204. * @return string The id attribute of the widget
  205. */
  206. public function renderId()
  207. {
  208. return $this->widget->generateId($this->parent ? $this->parent->getWidget()->generateName($this->name) : $this->name, $this->value);
  209. }
  210. /**
  211. * Returns true if the widget is hidden.
  212. *
  213. * @return Boolean true if the widget is hidden, false otherwise
  214. */
  215. public function isHidden()
  216. {
  217. return $this->widget->isHidden();
  218. }
  219. /**
  220. * Returns the widget name.
  221. *
  222. * @return mixed The widget name
  223. */
  224. public function getName()
  225. {
  226. return $this->name;
  227. }
  228. /**
  229. * Returns the widget value.
  230. *
  231. * @return mixed The widget value
  232. */
  233. public function getValue()
  234. {
  235. return $this->value;
  236. }
  237. /**
  238. * Returns the wrapped widget.
  239. *
  240. * @return sfWidget A sfWidget instance
  241. */
  242. public function getWidget()
  243. {
  244. return $this->widget;
  245. }
  246. /**
  247. * Returns the parent form field.
  248. *
  249. * @return sfFormField A sfFormField instance
  250. */
  251. public function getParent()
  252. {
  253. return $this->parent;
  254. }
  255. /**
  256. * Returns the error for this field.
  257. *
  258. * @return sfValidatorError A sfValidatorError instance
  259. */
  260. public function getError()
  261. {
  262. return $this->error;
  263. }
  264. /**
  265. * Returns true is the field has an error.
  266. *
  267. * @return Boolean true if the field has some errors, false otherwise
  268. */
  269. public function hasError()
  270. {
  271. return !is_null($this->error) && count($this->error);
  272. }
  273. }