form.php 13 KB

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