form.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. * 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. * Unless specified, the "accept-charset" attribute will be set to the application encoding.
  20. *
  21. * <code>
  22. * // Open a "POST" form to the current request URI
  23. * echo Form::open();
  24. *
  25. * // Open a "POST" form to a given URI
  26. * echo Form::open('user/profile');
  27. *
  28. * // Open a "PUT" form to a given URI
  29. * echo Form::open('user/profile', 'put');
  30. *
  31. * // Open a form that has HTML attributes
  32. * echo Form::open('user/profile', 'post', array('class' => 'profile'));
  33. * </code>
  34. *
  35. * @param string $action
  36. * @param string $method
  37. * @param array $attributes
  38. * @param bool $https
  39. * @return string
  40. */
  41. public static function open($action = null, $method = 'POST', $attributes = array(), $https = false)
  42. {
  43. $attributes['method'] = static::method($method);
  44. $attributes['action'] = static::action($action, $https);
  45. if ( ! array_key_exists('accept-charset', $attributes))
  46. {
  47. $attributes['accept-charset'] = Config::get('application.encoding');
  48. }
  49. $append = ($method == 'PUT' or $method == 'DELETE') ? static::hidden(Request::spoofer, $method) : '';
  50. return '<form'.HTML::attributes($attributes).'>'.$append.PHP_EOL;
  51. }
  52. /**
  53. * Determine the appropriate request method to use for a form.
  54. *
  55. * Since PUT and DELETE requests are spoofed using POST requests, we will substitute
  56. * POST for any PUT or DELETE methods. Otherwise, the specified method will be used.
  57. *
  58. * @param string $method
  59. * @return string
  60. */
  61. protected static function method($method)
  62. {
  63. return strtoupper(($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method);
  64. }
  65. /**
  66. * Determine the appropriate action parameter to use for a form.
  67. *
  68. * If no action is specified, the current request URI will be used.
  69. *
  70. * @param string $action
  71. * @param bool $https
  72. * @return string
  73. */
  74. protected static function action($action, $https)
  75. {
  76. return HTML::entities(URL::to(((is_null($action)) ? Request::uri() : $action), $https));
  77. }
  78. /**
  79. * Open a HTML form with a HTTPS action URI.
  80. *
  81. * @param string $action
  82. * @param string $method
  83. * @param array $attributes
  84. * @return string
  85. */
  86. public static function open_secure($action = null, $method = 'POST', $attributes = array())
  87. {
  88. return static::open($action, $method, $attributes, true);
  89. }
  90. /**
  91. * Open a HTML form that accepts file uploads.
  92. *
  93. * @param string $action
  94. * @param string $method
  95. * @param array $attributes
  96. * @param bool $https
  97. * @return string
  98. */
  99. public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
  100. {
  101. $attributes['enctype'] = 'multipart/form-data';
  102. return static::open($action, $method, $attributes, $https);
  103. }
  104. /**
  105. * Open a HTML form that accepts file uploads with a HTTPS action URI.
  106. *
  107. * @param string $action
  108. * @param string $method
  109. * @param array $attributes
  110. * @return string
  111. */
  112. public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
  113. {
  114. return static::open_for_files($action, $method, $attributes, true);
  115. }
  116. /**
  117. * Close a HTML form.
  118. *
  119. * @return string
  120. */
  121. public static function close()
  122. {
  123. return '</form>';
  124. }
  125. /**
  126. * Generate a hidden field containing the current CSRF token.
  127. *
  128. * @return string
  129. */
  130. public static function token()
  131. {
  132. return static::input('hidden', 'csrf_token', Session::token());
  133. }
  134. /**
  135. * Create a HTML label element.
  136. *
  137. * <code>
  138. * // Create a label for the "email" input element
  139. * echo Form::label('email', 'E-Mail Address');
  140. * </code>
  141. *
  142. * @param string $name
  143. * @param string $value
  144. * @param array $attributes
  145. * @return string
  146. */
  147. public static function label($name, $value, $attributes = array())
  148. {
  149. static::$labels[] = $name;
  150. return '<label for="'.$name.'"'.HTML::attributes($attributes).'>'.HTML::entities($value).'</label>'.PHP_EOL;
  151. }
  152. /**
  153. * Create a HTML input element.
  154. *
  155. * If an ID attribute is not specified and a label has been generated matching the input
  156. * element name, the label name will be used as the element ID.
  157. *
  158. * <code>
  159. * // Create a "text" input element named "email"
  160. * echo Form::input('text', 'email');
  161. *
  162. * // Create an input element with a specified default value
  163. * echo Form::input('text', 'email', 'example@gmail.com');
  164. * </code>
  165. *
  166. * @param string $name
  167. * @param mixed $value
  168. * @param array $attributes
  169. * @return string
  170. */
  171. public static function input($type, $name, $value = null, $attributes = array())
  172. {
  173. $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
  174. $id = static::id($name, $attributes);
  175. $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
  176. return '<input'.HTML::attributes($attributes).'>'.PHP_EOL;
  177. }
  178. /**
  179. * Create a HTML text input element.
  180. *
  181. * @param string $name
  182. * @param string $value
  183. * @param array $attributes
  184. * @return string
  185. */
  186. public static function text($name, $value = null, $attributes = array())
  187. {
  188. return static::input('text', $name, $value, $attributes);
  189. }
  190. /**
  191. * Create a HTML password input element.
  192. *
  193. * @param string $name
  194. * @param array $attributes
  195. * @return string
  196. */
  197. public static function password($name, $attributes = array())
  198. {
  199. return static::input('password', $name, null, $attributes);
  200. }
  201. /**
  202. * Create a HTML hidden input element.
  203. *
  204. * @param string $name
  205. * @param string $value
  206. * @param array $attributes
  207. * @return string
  208. */
  209. public static function hidden($name, $value = null, $attributes = array())
  210. {
  211. return static::input('hidden', $name, $value, $attributes);
  212. }
  213. /**
  214. * Create a HTML search input element.
  215. *
  216. * @param string $name
  217. * @param string $value
  218. * @param array $attributes
  219. * @return string
  220. */
  221. public static function search($name, $value = null, $attributes = array())
  222. {
  223. return static::input('search', $name, $value, $attributes);
  224. }
  225. /**
  226. * Create a HTML email input element.
  227. *
  228. * @param string $name
  229. * @param string $value
  230. * @param array $attributes
  231. * @return string
  232. */
  233. public static function email($name, $value = null, $attributes = array())
  234. {
  235. return static::input('email', $name, $value, $attributes);
  236. }
  237. /**
  238. * Create a HTML telephone input element.
  239. *
  240. * @param string $name
  241. * @param string $value
  242. * @param array $attributes
  243. * @return string
  244. */
  245. public static function telephone($name, $value = null, $attributes = array())
  246. {
  247. return static::input('tel', $name, $value, $attributes);
  248. }
  249. /**
  250. * Create a HTML URL input element.
  251. *
  252. * @param string $name
  253. * @param string $value
  254. * @param array $attributes
  255. * @return string
  256. */
  257. public static function url($name, $value = null, $attributes = array())
  258. {
  259. return static::input('url', $name, $value, $attributes);
  260. }
  261. /**
  262. * Create a HTML number input element.
  263. *
  264. * @param string $name
  265. * @param string $value
  266. * @param array $attributes
  267. * @return string
  268. */
  269. public static function number($name, $value = null, $attributes = array())
  270. {
  271. return static::input('number', $name, $value, $attributes);
  272. }
  273. /**
  274. * Create a HTML file input element.
  275. *
  276. * @param string $name
  277. * @param array $attributes
  278. * @return string
  279. */
  280. public static function file($name, $attributes = array())
  281. {
  282. return static::input('file', $name, null, $attributes);
  283. }
  284. /**
  285. * Create a HTML textarea element.
  286. *
  287. * @param string $name
  288. * @param string $value
  289. * @param array $attributes
  290. * @return string
  291. */
  292. public static function textarea($name, $value = '', $attributes = array())
  293. {
  294. $attributes = array_merge($attributes, array('id' => static::id($name, $attributes), 'name' => $name));
  295. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  296. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  297. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
  298. }
  299. /**
  300. * Create a HTML select element.
  301. *
  302. * <code>
  303. * // Create a HTML select element filled with options
  304. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
  305. *
  306. * // Create a select element with a default selected value
  307. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
  308. * </code>
  309. *
  310. * @param string $name
  311. * @param array $options
  312. * @param string $selected
  313. * @param array $attributes
  314. * @return string
  315. */
  316. public static function select($name, $options = array(), $selected = null, $attributes = array())
  317. {
  318. $attributes['id'] = static::id($name, $attributes);
  319. $attributes['name'] = $name;
  320. $html = array();
  321. foreach ($options as $value => $display)
  322. {
  323. $html[] = static::option($value, $display, $selected);
  324. }
  325. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
  326. }
  327. /**
  328. * Create a HTML select element option.
  329. *
  330. * @param string $value
  331. * @param string $display
  332. * @return string $selected
  333. * @return string
  334. */
  335. protected static function option($value, $display, $selected)
  336. {
  337. $selected = ($value === $selected) ? 'selected' : null;
  338. $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
  339. return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
  340. }
  341. /**
  342. * Create a HTML checkbox input element.
  343. *
  344. * <code>
  345. * // Create a checkbox element
  346. * echo Form::checkbox('terms', 'yes');
  347. *
  348. * // Create a checkbox that is selected by default
  349. * echo Form::checkbox('terms', 'yes', true);
  350. * </code>
  351. *
  352. * @param string $name
  353. * @param string $value
  354. * @param bool $checked
  355. * @param array $attributes
  356. * @return string
  357. */
  358. public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
  359. {
  360. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  361. }
  362. /**
  363. * Create a HTML radio button input element.
  364. *
  365. * <code>
  366. * // Create a radio button element
  367. * echo Form::radio('drinks', 'Milk');
  368. *
  369. * // Create a radio button that is selected by default
  370. * echo Form::radio('drinks', 'Milk', true);
  371. * </code>
  372. *
  373. * @param string $name
  374. * @param string $value
  375. * @param bool $checked
  376. * @param array $attributes
  377. * @return string
  378. */
  379. public static function radio($name, $value = null, $checked = false, $attributes = array())
  380. {
  381. if (is_null($value)) $value = $name;
  382. return static::checkable('radio', $name, $value, $checked, $attributes);
  383. }
  384. /**
  385. * Create a checkable input element.
  386. *
  387. * @param string $type
  388. * @param string $name
  389. * @param string $value
  390. * @param bool $checked
  391. * @param array $attributes
  392. * @return string
  393. */
  394. protected static function checkable($type, $name, $value, $checked, $attributes)
  395. {
  396. if ($checked) $attributes['checked'] = 'checked';
  397. $attributes['id'] = static::id($name, $attributes);
  398. return static::input($type, $name, $value, $attributes);
  399. }
  400. /**
  401. * Create a HTML submit input element.
  402. *
  403. * @param string $value
  404. * @param array $attributes
  405. * @return string
  406. */
  407. public static function submit($value, $attributes = array())
  408. {
  409. return static::input('submit', null, $value, $attributes);
  410. }
  411. /**
  412. * Create a HTML reset input element.
  413. *
  414. * @param string $value
  415. * @param array $attributes
  416. * @return string
  417. */
  418. public static function reset($value, $attributes = array())
  419. {
  420. return static::input('reset', null, $value, $attributes);
  421. }
  422. /**
  423. * Create a HTML image input element.
  424. *
  425. * The URL::to_asset method will be called on the given URL.
  426. *
  427. * <code>
  428. * // Create an image input element
  429. * echo Form::image('img/submit.png');
  430. * </code>
  431. *
  432. * @param string $url
  433. * @param array $attributes
  434. * @return string
  435. */
  436. public static function image($url, $name = null, $attributes = array())
  437. {
  438. $attributes['src'] = URL::to_asset($url);
  439. return static::input('image', $name, null, $attributes);
  440. }
  441. /**
  442. * Create a HTML button element.
  443. *
  444. * @param string $name
  445. * @param string $value
  446. * @param array $attributes
  447. * @return string
  448. */
  449. public static function button($value, $attributes = array())
  450. {
  451. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
  452. }
  453. /**
  454. * Determine the ID attribute for a form element.
  455. *
  456. * An explicitly specified ID in the attributes takes first precedence, then
  457. * the label names will be checked for a label matching the element name.
  458. *
  459. * @param string $name
  460. * @param array $attributes
  461. * @return mixed
  462. */
  463. protected static function id($name, $attributes)
  464. {
  465. if (array_key_exists('id', $attributes))
  466. {
  467. return $attributes['id'];
  468. }
  469. if (in_array($name, static::$labels))
  470. {
  471. return $name;
  472. }
  473. }
  474. }