form.php 13 KB

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