form.php 13 KB

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