form.php 13 KB

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