form.php 13 KB

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