form.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php namespace Laravel;
  2. class Form {
  3. /**
  4. * All of the label names that have been created.
  5. *
  6. * These names are stored so that input elements can automatically be assigned
  7. * an ID based on the corresponding label name.
  8. *
  9. * @var array
  10. */
  11. protected static $labels = array();
  12. /**
  13. * Open a HTML form.
  14. *
  15. * Note: If PUT or DELETE is specified as the form method, a hidden input field will be generated
  16. * containing the request method. PUT and DELETE are not supported by HTML forms, so the
  17. * hidden field will allow us to "spoof" PUT and DELETE requests.
  18. *
  19. * @param string $action
  20. * @param string $method
  21. * @param array $attributes
  22. * @param bool $https
  23. * @return string
  24. */
  25. public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
  26. {
  27. list($attributes['action'], $attributes['method']) = array(static::action($action, $https), static::method($method));
  28. if ( ! array_key_exists('accept-charset', $attributes))
  29. {
  30. $attributes['accept-charset'] = Config::get('application.encoding');
  31. }
  32. $append = ($method == 'PUT' or $method == 'DELETE') ? static::hidden(Request::spoofer, $method) : '';
  33. return '<form'.HTML::attributes($attributes).'>'.$append.PHP_EOL;
  34. }
  35. /**
  36. * Determine the appropriate request method to use for a form.
  37. *
  38. * Since PUT and DELETE requests are spoofed using POST requests, we will substitute
  39. * POST for any PUT or DELETE methods. Otherwise, the specified method will be used.
  40. *
  41. * @param string $method
  42. * @return string
  43. */
  44. protected static function method($method)
  45. {
  46. return strtoupper(($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method);
  47. }
  48. /**
  49. * Determine the appropriate action parameter to use for a form.
  50. *
  51. * If no action is specified, the current request URI will be used.
  52. *
  53. * @param string $action
  54. * @param bool $https
  55. * @return string
  56. */
  57. protected static function action($action, $https)
  58. {
  59. $uri = IoC::container()->resolve('laravel.uri')->get();
  60. return HTML::entities(URL::to(((is_null($action)) ? $uri : $action), $https));
  61. }
  62. /**
  63. * Open a HTML form with a HTTPS action URI.
  64. *
  65. * @param string $action
  66. * @param string $method
  67. * @param array $attributes
  68. * @return string
  69. */
  70. public static function open_secure($action = null, $method = 'POST', $attributes = array())
  71. {
  72. return static::open($action, $method, $attributes, true);
  73. }
  74. /**
  75. * Open a HTML form that accepts file uploads.
  76. *
  77. * @param string $action
  78. * @param string $method
  79. * @param array $attributes
  80. * @param bool $https
  81. * @return string
  82. */
  83. public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
  84. {
  85. $attributes['enctype'] = 'multipart/form-data';
  86. return static::open($action, $method, $attributes, $https);
  87. }
  88. /**
  89. * Open a HTML form that accepts file uploads with a HTTPS action URI.
  90. *
  91. * @param string $action
  92. * @param string $method
  93. * @param array $attributes
  94. * @return string
  95. */
  96. public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
  97. {
  98. return static::open_for_files($action, $method, $attributes, true);
  99. }
  100. /**
  101. * Close a HTML form.
  102. *
  103. * @return string
  104. */
  105. public static function close()
  106. {
  107. return '</form>';
  108. }
  109. /**
  110. * Generate a hidden field containing the current CSRF token.
  111. *
  112. * @return string
  113. */
  114. public static function token()
  115. {
  116. return static::input('hidden', 'csrf_token', static::raw_token());
  117. }
  118. /**
  119. * Get the CSRF token for the current session.
  120. *
  121. * @return string
  122. */
  123. public static function raw_token()
  124. {
  125. if (Config::get('session.driver') == '')
  126. {
  127. throw new \Exception("A session driver must be specified before using CSRF tokens.");
  128. }
  129. return IoC::container()->resolve('laravel.session')->get('csrf_token');
  130. }
  131. /**
  132. * Create a HTML label element.
  133. *
  134. * @param string $name
  135. * @param string $value
  136. * @param array $attributes
  137. * @return string
  138. */
  139. public static function label($name, $value, $attributes = array())
  140. {
  141. static::$labels[] = $name;
  142. return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
  143. }
  144. /**
  145. * Create a HTML input element.
  146. *
  147. * If an ID attribute is not specified and a label has been generated matching the input
  148. * element name, the label name will be used as the element ID.
  149. *
  150. * @param string $name
  151. * @param mixed $value
  152. * @param array $attributes
  153. * @return string
  154. */
  155. public static function input($type, $name, $value = null, $attributes = array())
  156. {
  157. $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
  158. $id = static::id($name, $attributes);
  159. return '<input'.HTML::attributes(array_merge($attributes, compact('type', 'name', 'value', 'id'))).'>'.PHP_EOL;
  160. }
  161. /**
  162. * Create a HTML text input element.
  163. *
  164. * @param string $name
  165. * @param string $value
  166. * @param array $attributes
  167. * @return string
  168. */
  169. public static function text($name, $value = null, $attributes = array())
  170. {
  171. return static::input('text', $name, $value, $attributes);
  172. }
  173. /**
  174. * Create a HTML password input element.
  175. *
  176. * @param string $name
  177. * @param array $attributes
  178. * @return string
  179. */
  180. public static function password($name, $attributes = array())
  181. {
  182. return static::input('password', $name, null, $attributes);
  183. }
  184. /**
  185. * Create a HTML hidden input element.
  186. *
  187. * @param string $name
  188. * @param string $value
  189. * @param array $attributes
  190. * @return string
  191. */
  192. public static function hidden($name, $value = null, $attributes = array())
  193. {
  194. return static::input('hidden', $name, $value, $attributes);
  195. }
  196. /**
  197. * Create a HTML search input element.
  198. *
  199. * @param string $name
  200. * @param string $value
  201. * @param array $attributes
  202. * @return string
  203. */
  204. public static function search($name, $value = null, $attributes = array())
  205. {
  206. return static::input('search', $name, $value, $attributes);
  207. }
  208. /**
  209. * Create a HTML email input element.
  210. *
  211. * @param string $name
  212. * @param string $value
  213. * @param array $attributes
  214. * @return string
  215. */
  216. public static function email($name, $value = null, $attributes = array())
  217. {
  218. return static::input('email', $name, $value, $attributes);
  219. }
  220. /**
  221. * Create a HTML telephone input element.
  222. *
  223. * @param string $name
  224. * @param string $value
  225. * @param array $attributes
  226. * @return string
  227. */
  228. public static function telephone($name, $value = null, $attributes = array())
  229. {
  230. return static::input('tel', $name, $value, $attributes);
  231. }
  232. /**
  233. * Create a HTML URL input element.
  234. *
  235. * @param string $name
  236. * @param string $value
  237. * @param array $attributes
  238. * @return string
  239. */
  240. public static function url($name, $value = null, $attributes = array())
  241. {
  242. return static::input('url', $name, $value, $attributes);
  243. }
  244. /**
  245. * Create a HTML number input element.
  246. *
  247. * @param string $name
  248. * @param string $value
  249. * @param array $attributes
  250. * @return string
  251. */
  252. public static function number($name, $value = null, $attributes = array())
  253. {
  254. return static::input('number', $name, $value, $attributes);
  255. }
  256. /**
  257. * Create a HTML file input element.
  258. *
  259. * @param string $name
  260. * @param array $attributes
  261. * @return string
  262. */
  263. public static function file($name, $attributes = array())
  264. {
  265. return static::input('file', $name, null, $attributes);
  266. }
  267. /**
  268. * Create a HTML textarea element.
  269. *
  270. * @param string $name
  271. * @param string $value
  272. * @param array $attributes
  273. * @return string
  274. */
  275. public static function textarea($name, $value = '', $attributes = array())
  276. {
  277. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
  278. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  279. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  280. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
  281. }
  282. /**
  283. * Create a HTML select element.
  284. *
  285. * @param string $name
  286. * @param array $options
  287. * @param string $selected
  288. * @param array $attributes
  289. * @return string
  290. */
  291. public static function select($name, $options = array(), $selected = null, $attributes = array())
  292. {
  293. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
  294. $html = array();
  295. foreach ($options as $value => $display)
  296. {
  297. $option_attributes = array('value' => HTML::entities($value), 'selected' => ($value == $selected) ? 'selected' : null);
  298. $html[] = '<option'.HTML::attributes($option_attributes).'>'.HTML::entities($display).'</option>';
  299. }
  300. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
  301. }
  302. /**
  303. * Create a HTML checkbox input element.
  304. *
  305. * @param string $name
  306. * @param string $value
  307. * @param bool $checked
  308. * @param array $attributes
  309. * @return string
  310. */
  311. public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
  312. {
  313. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  314. }
  315. /**
  316. * Create a HTML radio button input element.
  317. *
  318. * @param string $name
  319. * @param string $value
  320. * @param bool $checked
  321. * @param array $attributes
  322. * @return string
  323. */
  324. public static function radio($name, $value = null, $checked = false, $attributes = array())
  325. {
  326. if (is_null($value)) $value = $name;
  327. return static::checkable('radio', $name, $value, $checked, $attributes);
  328. }
  329. /**
  330. * Create a checkable input element.
  331. *
  332. * @param string $type
  333. * @param string $name
  334. * @param string $value
  335. * @param bool $checked
  336. * @param array $attributes
  337. * @return string
  338. */
  339. protected static function checkable($type, $name, $value, $checked, $attributes)
  340. {
  341. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'checked' => ($checked) ? 'checked' : null));
  342. return static::input($type, $name, $value, $attributes);
  343. }
  344. /**
  345. * Create a HTML submit input element.
  346. *
  347. * @param string $value
  348. * @param array $attributes
  349. * @return string
  350. */
  351. public static function submit($value, $attributes = array())
  352. {
  353. return static::input('submit', null, $value, $attributes);
  354. }
  355. /**
  356. * Create a HTML reset input element.
  357. *
  358. * @param string $value
  359. * @param array $attributes
  360. * @return string
  361. */
  362. public static function reset($value, $attributes = array())
  363. {
  364. return static::input('reset', null, $value, $attributes);
  365. }
  366. /**
  367. * Create a HTML image input element.
  368. *
  369. * @param string $url
  370. * @param array $attributes
  371. * @return string
  372. */
  373. public static function image($url, $name = null, $attributes = array())
  374. {
  375. $attributes['src'] = URL::to_asset($url);
  376. return static::input('image', $name, null, $attributes);
  377. }
  378. /**
  379. * Create a HTML button element.
  380. *
  381. * @param string $name
  382. * @param string $value
  383. * @param array $attributes
  384. * @return string
  385. */
  386. public static function button($value, $attributes = array())
  387. {
  388. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
  389. }
  390. /**
  391. * Determine the ID attribute for a form element.
  392. *
  393. * An explicitly specified ID in the attributes takes first precedence, then
  394. * the label names will be checked for a label matching the element name.
  395. *
  396. * @param string $name
  397. * @param array $attributes
  398. * @return mixed
  399. */
  400. protected static function id($name, $attributes)
  401. {
  402. if (array_key_exists('id', $attributes)) return $attributes['id'];
  403. if (in_array($name, static::$labels)) return $name;
  404. }
  405. }