form.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 $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 date input element.
  275. *
  276. * @param string $name
  277. * @param string $value
  278. * @param array $attributes
  279. * @return string
  280. */
  281. public static function date($name, $value = null, $attributes = array())
  282. {
  283. return static::input('date', $name, $value, $attributes);
  284. }
  285. /**
  286. * Create a HTML file input element.
  287. *
  288. * @param string $name
  289. * @param array $attributes
  290. * @return string
  291. */
  292. public static function file($name, $attributes = array())
  293. {
  294. return static::input('file', $name, null, $attributes);
  295. }
  296. /**
  297. * Create a HTML textarea element.
  298. *
  299. * @param string $name
  300. * @param string $value
  301. * @param array $attributes
  302. * @return string
  303. */
  304. public static function textarea($name, $value = '', $attributes = array())
  305. {
  306. $attributes['name'] = $name;
  307. $attributes['id'] = static::id($name, $attributes);
  308. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  309. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  310. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>'.PHP_EOL;
  311. }
  312. /**
  313. * Create a HTML select element.
  314. *
  315. * <code>
  316. * // Create a HTML select element filled with options
  317. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
  318. *
  319. * // Create a select element with a default selected value
  320. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
  321. * </code>
  322. *
  323. * @param string $name
  324. * @param array $options
  325. * @param string $selected
  326. * @param array $attributes
  327. * @return string
  328. */
  329. public static function select($name, $options = array(), $selected = null, $attributes = array())
  330. {
  331. $attributes['id'] = static::id($name, $attributes);
  332. $attributes['name'] = $name;
  333. $html = array();
  334. foreach ($options as $value => $display)
  335. {
  336. $html[] = static::option($value, $display, $selected);
  337. }
  338. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
  339. }
  340. /**
  341. * Create a HTML select element option.
  342. *
  343. * @param string $value
  344. * @param string $display
  345. * @return string $selected
  346. * @return string
  347. */
  348. protected static function option($value, $display, $selected)
  349. {
  350. if (is_array($selected))
  351. {
  352. $selected = (in_array($value, $selected)) ? 'selected' : null;
  353. }
  354. else
  355. {
  356. $selected = ($value == $selected) ? 'selected' : null;
  357. }
  358. $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
  359. return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
  360. }
  361. /**
  362. * Create a HTML checkbox input element.
  363. *
  364. * <code>
  365. * // Create a checkbox element
  366. * echo Form::checkbox('terms', 'yes');
  367. *
  368. * // Create a checkbox that is selected by default
  369. * echo Form::checkbox('terms', 'yes', true);
  370. * </code>
  371. *
  372. * @param string $name
  373. * @param string $value
  374. * @param bool $checked
  375. * @param array $attributes
  376. * @return string
  377. */
  378. public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
  379. {
  380. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  381. }
  382. /**
  383. * Create a HTML radio button input element.
  384. *
  385. * <code>
  386. * // Create a radio button element
  387. * echo Form::radio('drinks', 'Milk');
  388. *
  389. * // Create a radio button that is selected by default
  390. * echo Form::radio('drinks', 'Milk', true);
  391. * </code>
  392. *
  393. * @param string $name
  394. * @param string $value
  395. * @param bool $checked
  396. * @param array $attributes
  397. * @return string
  398. */
  399. public static function radio($name, $value = null, $checked = false, $attributes = array())
  400. {
  401. if (is_null($value)) $value = $name;
  402. return static::checkable('radio', $name, $value, $checked, $attributes);
  403. }
  404. /**
  405. * Create a checkable input element.
  406. *
  407. * @param string $type
  408. * @param string $name
  409. * @param string $value
  410. * @param bool $checked
  411. * @param array $attributes
  412. * @return string
  413. */
  414. protected static function checkable($type, $name, $value, $checked, $attributes)
  415. {
  416. if ($checked) $attributes['checked'] = 'checked';
  417. $attributes['id'] = static::id($name, $attributes);
  418. return static::input($type, $name, $value, $attributes);
  419. }
  420. /**
  421. * Create a HTML submit input element.
  422. *
  423. * @param string $value
  424. * @param array $attributes
  425. * @return string
  426. */
  427. public static function submit($value, $attributes = array())
  428. {
  429. return static::input('submit', null, $value, $attributes);
  430. }
  431. /**
  432. * Create a HTML reset input element.
  433. *
  434. * @param string $value
  435. * @param array $attributes
  436. * @return string
  437. */
  438. public static function reset($value, $attributes = array())
  439. {
  440. return static::input('reset', null, $value, $attributes);
  441. }
  442. /**
  443. * Create a HTML image input element.
  444. *
  445. * <code>
  446. * // Create an image input element
  447. * echo Form::image('img/submit.png');
  448. * </code>
  449. *
  450. * @param string $url
  451. * @param array $attributes
  452. * @return string
  453. */
  454. public static function image($url, $name = null, $attributes = array())
  455. {
  456. $attributes['src'] = URL::to_asset($url);
  457. return static::input('image', $name, null, $attributes);
  458. }
  459. /**
  460. * Create a HTML button element.
  461. *
  462. * @param string $name
  463. * @param string $value
  464. * @param array $attributes
  465. * @return string
  466. */
  467. public static function button($value, $attributes = array())
  468. {
  469. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>'.PHP_EOL;
  470. }
  471. /**
  472. * Determine the ID attribute for a form element.
  473. *
  474. * @param string $name
  475. * @param array $attributes
  476. * @return mixed
  477. */
  478. protected static function id($name, $attributes)
  479. {
  480. // If an ID has been explicitly specified in the attributes, we will
  481. // use that ID. Otherwise, we will look for an ID in the array of
  482. // label names as this makes it convenient to give input elements
  483. // the same ID as their corresponding labels.
  484. if (array_key_exists('id', $attributes))
  485. {
  486. return $attributes['id'];
  487. }
  488. if (in_array($name, static::$labels))
  489. {
  490. return $name;
  491. }
  492. }
  493. }