form.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.5
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2013 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * Form Class
  15. *
  16. * Helper for creating forms with support for creating dynamic form objects.
  17. *
  18. * @package Fuel
  19. * @category Core
  20. */
  21. class Form
  22. {
  23. /*
  24. * @var Form_Instance the default form instance
  25. */
  26. protected static $instance;
  27. /**
  28. * When autoloaded this will method will be fired, load once and once only
  29. *
  30. * @return void
  31. */
  32. public static function _init()
  33. {
  34. \Config::load('form', true);
  35. static::$instance = static::forge('_default_', \Config::get('form'));
  36. }
  37. public static function forge($fieldset = 'default', array $config = array())
  38. {
  39. if (is_string($fieldset))
  40. {
  41. ($set = \Fieldset::instance($fieldset)) and $fieldset = $set;
  42. }
  43. if ($fieldset instanceof Fieldset)
  44. {
  45. if ($fieldset->form(false) != null)
  46. {
  47. throw new \DomainException('Form instance already exists, cannot be recreated. Use instance() instead of forge() to retrieve the existing instance.');
  48. }
  49. }
  50. return new \Form_Instance($fieldset, $config);
  51. }
  52. /**
  53. * Returns the 'default' instance of Form
  54. *
  55. * @param null|string $name
  56. * @return Form_Instance
  57. */
  58. public static function instance($name = null)
  59. {
  60. $fieldset = \Fieldset::instance($name);
  61. return $fieldset === false ? false : $fieldset->form();
  62. }
  63. /**
  64. * Create a form open tag
  65. *
  66. * @param string|array action string or array with more tag attribute settings
  67. * @return string
  68. */
  69. public static function open($attributes = array(), array $hidden = array())
  70. {
  71. return static::$instance->open($attributes, $hidden);
  72. }
  73. /**
  74. * Create a form close tag
  75. *
  76. * @return string
  77. */
  78. public static function close()
  79. {
  80. return static::$instance->close();
  81. }
  82. /**
  83. * Create a fieldset open tag
  84. *
  85. * @param array array with tag attribute settings
  86. * @param string string for the fieldset legend
  87. * @return string
  88. */
  89. public static function fieldset_open($attributes = array(), $legend = null)
  90. {
  91. return static::$instance->fieldset_open($attributes, $legend);
  92. }
  93. /**
  94. * Create a fieldset close tag
  95. *
  96. * @return string
  97. */
  98. public static function fieldset_close()
  99. {
  100. return static::$instance->fieldset_close();
  101. }
  102. /**
  103. * Create a form input
  104. *
  105. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  106. * @param string
  107. * @param array
  108. * @return string
  109. */
  110. public static function input($field, $value = null, array $attributes = array())
  111. {
  112. return static::$instance->input($field, $value, $attributes);
  113. }
  114. /**
  115. * Create a hidden field
  116. *
  117. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  118. * @param string
  119. * @param array
  120. * @return string
  121. */
  122. public static function hidden($field, $value = null, array $attributes = array())
  123. {
  124. return static::$instance->hidden($field, $value, $attributes);
  125. }
  126. /**
  127. * Create a password input field
  128. *
  129. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  130. * @param string
  131. * @param array
  132. * @return string
  133. */
  134. public static function password($field, $value = null, array $attributes = array())
  135. {
  136. return static::$instance->password($field, $value, $attributes);
  137. }
  138. /**
  139. * Create a radio button
  140. *
  141. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  142. * @param string
  143. * @param mixed either attributes (array) or bool/string to set checked status
  144. * @param array
  145. * @return string
  146. */
  147. public static function radio($field, $value = null, $checked = null, array $attributes = array())
  148. {
  149. return static::$instance->radio($field, $value, $checked, $attributes);
  150. }
  151. /**
  152. * Create a checkbox
  153. *
  154. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  155. * @param string
  156. * @param mixed either attributes (array) or bool/string to set checked status
  157. * @param array
  158. * @return string
  159. */
  160. public static function checkbox($field, $value = null, $checked = null, array $attributes = array())
  161. {
  162. return static::$instance->checkbox($field, $value, $checked, $attributes);
  163. }
  164. /**
  165. * Create a file upload input field
  166. *
  167. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  168. * @param array
  169. * @return string
  170. */
  171. public static function file($field, array $attributes = array())
  172. {
  173. return static::$instance->file($field, $attributes);
  174. }
  175. /**
  176. * Create a button
  177. *
  178. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  179. * @param string
  180. * @param array
  181. * @return string
  182. */
  183. public static function button($field, $value = null, array $attributes = array())
  184. {
  185. return static::$instance->button($field, $value, $attributes);
  186. }
  187. /**
  188. * Create a reset button
  189. *
  190. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  191. * @param string
  192. * @param array
  193. * @return string
  194. */
  195. public static function reset($field = 'reset', $value = 'Reset', array $attributes = array())
  196. {
  197. return static::$instance->reset($field, $value, $attributes);
  198. }
  199. /**
  200. * Create a submit button
  201. *
  202. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  203. * @param string
  204. * @param array
  205. * @return string
  206. */
  207. public static function submit($field = 'submit', $value = 'Submit', array $attributes = array())
  208. {
  209. return static::$instance->submit($field, $value, $attributes);
  210. }
  211. /**
  212. * Create a textarea field
  213. *
  214. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  215. * @param string
  216. * @param array
  217. * @return string
  218. */
  219. public static function textarea($field, $value = null, array $attributes = array())
  220. {
  221. return static::$instance->textarea($field, $value, $attributes);
  222. }
  223. /**
  224. * Select
  225. *
  226. * Generates a html select element based on the given parameters
  227. *
  228. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  229. * @param string selected value(s)
  230. * @param array array of options and option groups
  231. * @param array
  232. * @return string
  233. */
  234. public static function select($field, $values = null, array $options = array(), array $attributes = array())
  235. {
  236. return static::$instance->select($field, $values, $options, $attributes);
  237. }
  238. /**
  239. * Create a label field
  240. *
  241. * @param string|array either fieldname or full attributes array (when array other params are ignored)
  242. * @param string
  243. * @param array
  244. * @return string
  245. */
  246. public static function label($label, $id = null, array $attributes = array())
  247. {
  248. return static::$instance->label($label, $id, $attributes);
  249. }
  250. /**
  251. * Prep Value
  252. *
  253. * Prepares the value for display in the form
  254. *
  255. * @param string
  256. * @return string
  257. */
  258. public static function prep_value($value)
  259. {
  260. return static::$instance->prep_value($value);
  261. }
  262. /**
  263. * Attr to String
  264. *
  265. * Wraps the global attributes function and does some form specific work
  266. *
  267. * @param array $attr
  268. * @return string
  269. */
  270. protected static function attr_to_string($attr)
  271. {
  272. return static::$instance->attr_to_string($attr);
  273. }
  274. }