form.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. <?php namespace Laravel;
  2. class Form {
  3. /**
  4. * All of the label names that have been created.
  5. *
  6. * @var array
  7. */
  8. public static $labels = array();
  9. /**
  10. * The registered custom macros.
  11. *
  12. * @var array
  13. */
  14. public static $macros = array();
  15. /**
  16. * Registers a custom macro.
  17. *
  18. * @param string $name
  19. * @param Closure $macro
  20. * @return void
  21. */
  22. public static function macro($name, $macro)
  23. {
  24. static::$macros[$name] = $macro;
  25. }
  26. /**
  27. * Open a HTML form.
  28. *
  29. * <code>
  30. * // Open a "POST" form to the current request URI
  31. * echo Form::open();
  32. *
  33. * // Open a "POST" form to a given URI
  34. * echo Form::open('user/profile');
  35. *
  36. * // Open a "PUT" form to a given URI
  37. * echo Form::open('user/profile', 'put');
  38. *
  39. * // Open a form that has HTML attributes
  40. * echo Form::open('user/profile', 'post', array('class' => 'profile'));
  41. * </code>
  42. *
  43. * @param string $action
  44. * @param string $method
  45. * @param array $attributes
  46. * @param bool $https
  47. * @return string
  48. */
  49. public static function open($action = null, $method = 'POST', $attributes = array(), $https = null)
  50. {
  51. $method = strtoupper($method);
  52. $attributes['method'] = static::method($method);
  53. $attributes['action'] = static::action($action, $https);
  54. // If a character encoding has not been specified in the attributes, we will
  55. // use the default encoding as specified in the application configuration
  56. // file for the "accept-charset" attribute.
  57. if ( ! array_key_exists('accept-charset', $attributes))
  58. {
  59. $attributes['accept-charset'] = Config::get('application.encoding');
  60. }
  61. $append = '';
  62. // Since PUT and DELETE methods are not actually supported by HTML forms,
  63. // we'll create a hidden input element that contains the request method
  64. // and set the actual request method variable to POST.
  65. if ($method == 'PUT' or $method == 'DELETE')
  66. {
  67. $append = static::hidden(Request::spoofer, $method);
  68. }
  69. return '<form'.HTML::attributes($attributes).'>'.$append;
  70. }
  71. /**
  72. * Determine the appropriate request method to use for a form.
  73. *
  74. * @param string $method
  75. * @return string
  76. */
  77. protected static function method($method)
  78. {
  79. return ($method !== 'GET') ? 'POST' : $method;
  80. }
  81. /**
  82. * Determine the appropriate action parameter to use for a form.
  83. *
  84. * If no action is specified, the current request URI will be used.
  85. *
  86. * @param string $action
  87. * @param bool $https
  88. * @return string
  89. */
  90. protected static function action($action, $https)
  91. {
  92. $uri = (is_null($action)) ? URI::current() : $action;
  93. return HTML::entities(URL::to($uri, $https));
  94. }
  95. /**
  96. * Open a HTML form with a HTTPS action URI.
  97. *
  98. * @param string $action
  99. * @param string $method
  100. * @param array $attributes
  101. * @return string
  102. */
  103. public static function open_secure($action = null, $method = 'POST', $attributes = array())
  104. {
  105. return static::open($action, $method, $attributes, true);
  106. }
  107. /**
  108. * Open a HTML form that accepts file uploads.
  109. *
  110. * @param string $action
  111. * @param string $method
  112. * @param array $attributes
  113. * @param bool $https
  114. * @return string
  115. */
  116. public static function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = null)
  117. {
  118. $attributes['enctype'] = 'multipart/form-data';
  119. return static::open($action, $method, $attributes, $https);
  120. }
  121. /**
  122. * Open a HTML form that accepts file uploads with a HTTPS action URI.
  123. *
  124. * @param string $action
  125. * @param string $method
  126. * @param array $attributes
  127. * @return string
  128. */
  129. public static function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
  130. {
  131. return static::open_for_files($action, $method, $attributes, true);
  132. }
  133. /**
  134. * Close a HTML form.
  135. *
  136. * @return string
  137. */
  138. public static function close()
  139. {
  140. return '</form>';
  141. }
  142. /**
  143. * Generate a hidden field containing the current CSRF token.
  144. *
  145. * @return string
  146. */
  147. public static function token()
  148. {
  149. return static::input('hidden', Session::csrf_token, Session::token());
  150. }
  151. /**
  152. * Create a HTML label element.
  153. *
  154. * <code>
  155. * // Create a label for the "email" input element
  156. * echo Form::label('email', 'E-Mail Address');
  157. * </code>
  158. *
  159. * @param string $name
  160. * @param string $value
  161. * @param array $attributes
  162. * @return string
  163. */
  164. public static function label($name, $value, $attributes = array(), $escape_html = true)
  165. {
  166. static::$labels[] = $name;
  167. $attributes = HTML::attributes($attributes);
  168. if ($escape_html) {
  169. $value = HTML::entities($value);
  170. }
  171. return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
  172. }
  173. /**
  174. * Create a HTML input element.
  175. *
  176. * <code>
  177. * // Create a "text" input element named "email"
  178. * echo Form::input('text', 'email');
  179. *
  180. * // Create an input element with a specified default value
  181. * echo Form::input('text', 'email', 'example@gmail.com');
  182. * </code>
  183. *
  184. * @param string $type
  185. * @param string $name
  186. * @param mixed $value
  187. * @param array $attributes
  188. * @return string
  189. */
  190. public static function input($type, $name, $value = null, $attributes = array())
  191. {
  192. $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
  193. $id = static::id($name, $attributes);
  194. $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
  195. return '<input'.HTML::attributes($attributes).'>';
  196. }
  197. /**
  198. * Create a HTML text input element.
  199. *
  200. * @param string $name
  201. * @param string $value
  202. * @param array $attributes
  203. * @return string
  204. */
  205. public static function text($name, $value = null, $attributes = array())
  206. {
  207. return static::input('text', $name, $value, $attributes);
  208. }
  209. /**
  210. * Create a HTML password input element.
  211. *
  212. * @param string $name
  213. * @param array $attributes
  214. * @return string
  215. */
  216. public static function password($name, $attributes = array())
  217. {
  218. return static::input('password', $name, null, $attributes);
  219. }
  220. /**
  221. * Create a HTML hidden input element.
  222. *
  223. * @param string $name
  224. * @param string $value
  225. * @param array $attributes
  226. * @return string
  227. */
  228. public static function hidden($name, $value = null, $attributes = array())
  229. {
  230. return static::input('hidden', $name, $value, $attributes);
  231. }
  232. /**
  233. * Create a HTML search input element.
  234. *
  235. * @param string $name
  236. * @param string $value
  237. * @param array $attributes
  238. * @return string
  239. */
  240. public static function search($name, $value = null, $attributes = array())
  241. {
  242. return static::input('search', $name, $value, $attributes);
  243. }
  244. /**
  245. * Create a HTML email input element.
  246. *
  247. * @param string $name
  248. * @param string $value
  249. * @param array $attributes
  250. * @return string
  251. */
  252. public static function email($name, $value = null, $attributes = array())
  253. {
  254. return static::input('email', $name, $value, $attributes);
  255. }
  256. /**
  257. * Create a HTML telephone input element.
  258. *
  259. * @param string $name
  260. * @param string $value
  261. * @param array $attributes
  262. * @return string
  263. */
  264. public static function telephone($name, $value = null, $attributes = array())
  265. {
  266. return static::input('tel', $name, $value, $attributes);
  267. }
  268. /**
  269. * Create a HTML URL input element.
  270. *
  271. * @param string $name
  272. * @param string $value
  273. * @param array $attributes
  274. * @return string
  275. */
  276. public static function url($name, $value = null, $attributes = array())
  277. {
  278. return static::input('url', $name, $value, $attributes);
  279. }
  280. /**
  281. * Create a HTML number input element.
  282. *
  283. * @param string $name
  284. * @param string $value
  285. * @param array $attributes
  286. * @return string
  287. */
  288. public static function number($name, $value = null, $attributes = array())
  289. {
  290. return static::input('number', $name, $value, $attributes);
  291. }
  292. /**
  293. * Create a HTML date input element.
  294. *
  295. * @param string $name
  296. * @param string $value
  297. * @param array $attributes
  298. * @return string
  299. */
  300. public static function date($name, $value = null, $attributes = array())
  301. {
  302. return static::input('date', $name, $value, $attributes);
  303. }
  304. /**
  305. * Create a HTML file input element.
  306. *
  307. * @param string $name
  308. * @param array $attributes
  309. * @return string
  310. */
  311. public static function file($name, $attributes = array())
  312. {
  313. return static::input('file', $name, null, $attributes);
  314. }
  315. /**
  316. * Create a HTML textarea element.
  317. *
  318. * @param string $name
  319. * @param string $value
  320. * @param array $attributes
  321. * @return string
  322. */
  323. public static function textarea($name, $value = '', $attributes = array())
  324. {
  325. $attributes['name'] = $name;
  326. $attributes['id'] = static::id($name, $attributes);
  327. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  328. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  329. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
  330. }
  331. /**
  332. * Create a HTML select element.
  333. *
  334. * <code>
  335. * // Create a HTML select element filled with options
  336. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
  337. *
  338. * // Create a select element with a default selected value
  339. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
  340. * </code>
  341. *
  342. * @param string $name
  343. * @param array $options
  344. * @param string $selected
  345. * @param array $attributes
  346. * @return string
  347. */
  348. public static function select($name, $options = array(), $selected = null, $attributes = array())
  349. {
  350. $attributes['id'] = static::id($name, $attributes);
  351. $attributes['name'] = $name;
  352. $html = array();
  353. foreach ($options as $value => $display)
  354. {
  355. if (is_array($display))
  356. {
  357. $html[] = static::optgroup($display, $value, $selected);
  358. }
  359. else
  360. {
  361. $html[] = static::option($value, $display, $selected);
  362. }
  363. }
  364. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
  365. }
  366. /**
  367. * Create a HTML select element optgroup.
  368. *
  369. * @param array $options
  370. * @param string $label
  371. * @param string $selected
  372. * @return string
  373. */
  374. protected static function optgroup($options, $label, $selected)
  375. {
  376. $html = array();
  377. foreach ($options as $value => $display)
  378. {
  379. $html[] = static::option($value, $display, $selected);
  380. }
  381. return '<optgroup label="'.HTML::entities($label).'">'.implode('', $html).'</optgroup>';
  382. }
  383. /**
  384. * Create a HTML select element option.
  385. *
  386. * @param string $value
  387. * @param string $display
  388. * @param string $selected
  389. * @return string
  390. */
  391. protected static function option($value, $display, $selected)
  392. {
  393. if (is_array($selected))
  394. {
  395. $selected = (in_array($value, $selected)) ? 'selected' : null;
  396. }
  397. else
  398. {
  399. $selected = ((string) $value == (string) $selected) ? 'selected' : null;
  400. }
  401. $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
  402. return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
  403. }
  404. /**
  405. * Create a HTML checkbox input element.
  406. *
  407. * <code>
  408. * // Create a checkbox element
  409. * echo Form::checkbox('terms', 'yes');
  410. *
  411. * // Create a checkbox that is selected by default
  412. * echo Form::checkbox('terms', 'yes', true);
  413. * </code>
  414. *
  415. * @param string $name
  416. * @param string $value
  417. * @param bool $checked
  418. * @param array $attributes
  419. * @return string
  420. */
  421. public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
  422. {
  423. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  424. }
  425. /**
  426. * Create a HTML radio button input element.
  427. *
  428. * <code>
  429. * // Create a radio button element
  430. * echo Form::radio('drinks', 'Milk');
  431. *
  432. * // Create a radio button that is selected by default
  433. * echo Form::radio('drinks', 'Milk', true);
  434. * </code>
  435. *
  436. * @param string $name
  437. * @param string $value
  438. * @param bool $checked
  439. * @param array $attributes
  440. * @return string
  441. */
  442. public static function radio($name, $value = null, $checked = false, $attributes = array())
  443. {
  444. if (is_null($value)) $value = $name;
  445. return static::checkable('radio', $name, $value, $checked, $attributes);
  446. }
  447. /**
  448. * Create a checkable input element.
  449. *
  450. * @param string $type
  451. * @param string $name
  452. * @param string $value
  453. * @param bool $checked
  454. * @param array $attributes
  455. * @return string
  456. */
  457. protected static function checkable($type, $name, $value, $checked, $attributes)
  458. {
  459. if ($checked) $attributes['checked'] = 'checked';
  460. $attributes['id'] = static::id($name, $attributes);
  461. return static::input($type, $name, $value, $attributes);
  462. }
  463. /**
  464. * Create a HTML submit input element.
  465. *
  466. * @param string $value
  467. * @param array $attributes
  468. * @return string
  469. */
  470. public static function submit($value = null, $attributes = array())
  471. {
  472. return static::input('submit', null, $value, $attributes);
  473. }
  474. /**
  475. * Create a HTML reset input element.
  476. *
  477. * @param string $value
  478. * @param array $attributes
  479. * @return string
  480. */
  481. public static function reset($value = null, $attributes = array())
  482. {
  483. return static::input('reset', null, $value, $attributes);
  484. }
  485. /**
  486. * Create a HTML image input element.
  487. *
  488. * <code>
  489. * // Create an image input element
  490. * echo Form::image('img/submit.png');
  491. * </code>
  492. *
  493. * @param string $url
  494. * @param string $name
  495. * @param array $attributes
  496. * @return string
  497. */
  498. public static function image($url, $name = null, $attributes = array())
  499. {
  500. $attributes['src'] = URL::to_asset($url);
  501. return static::input('image', $name, null, $attributes);
  502. }
  503. /**
  504. * Create a HTML button element.
  505. *
  506. * @param string $value
  507. * @param array $attributes
  508. * @return string
  509. */
  510. public static function button($value = null, $attributes = array())
  511. {
  512. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
  513. }
  514. /**
  515. * Determine the ID attribute for a form element.
  516. *
  517. * @param string $name
  518. * @param array $attributes
  519. * @return mixed
  520. */
  521. protected static function id($name, $attributes)
  522. {
  523. // If an ID has been explicitly specified in the attributes, we will
  524. // use that ID. Otherwise, we will look for an ID in the array of
  525. // label names so labels and their elements have the same ID.
  526. if (array_key_exists('id', $attributes))
  527. {
  528. return $attributes['id'];
  529. }
  530. if (in_array($name, static::$labels))
  531. {
  532. return $name;
  533. }
  534. }
  535. /**
  536. * Dynamically handle calls to custom macros.
  537. *
  538. * @param string $method
  539. * @param array $parameters
  540. * @return mixed
  541. */
  542. public static function __callStatic($method, $parameters)
  543. {
  544. if (isset(static::$macros[$method]))
  545. {
  546. return call_user_func_array(static::$macros[$method], $parameters);
  547. }
  548. throw new \Exception("Method [$method] does not exist.");
  549. }
  550. }