form.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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())
  165. {
  166. static::$labels[] = $name;
  167. $attributes = HTML::attributes($attributes);
  168. $value = HTML::entities($value);
  169. return '<label for="'.$name.'"'.$attributes.'>'.$value.'</label>';
  170. }
  171. /**
  172. * Create a HTML input element.
  173. *
  174. * <code>
  175. * // Create a "text" input element named "email"
  176. * echo Form::input('text', 'email');
  177. *
  178. * // Create an input element with a specified default value
  179. * echo Form::input('text', 'email', 'example@gmail.com');
  180. * </code>
  181. *
  182. * @param string $type
  183. * @param string $name
  184. * @param mixed $value
  185. * @param array $attributes
  186. * @return string
  187. */
  188. public static function input($type, $name, $value = null, $attributes = array())
  189. {
  190. $name = (isset($attributes['name'])) ? $attributes['name'] : $name;
  191. $id = static::id($name, $attributes);
  192. $attributes = array_merge($attributes, compact('type', 'name', 'value', 'id'));
  193. return '<input'.HTML::attributes($attributes).'>';
  194. }
  195. /**
  196. * Create a HTML text input element.
  197. *
  198. * @param string $name
  199. * @param string $value
  200. * @param array $attributes
  201. * @return string
  202. */
  203. public static function text($name, $value = null, $attributes = array())
  204. {
  205. return static::input('text', $name, $value, $attributes);
  206. }
  207. /**
  208. * Create a HTML password input element.
  209. *
  210. * @param string $name
  211. * @param array $attributes
  212. * @return string
  213. */
  214. public static function password($name, $attributes = array())
  215. {
  216. return static::input('password', $name, null, $attributes);
  217. }
  218. /**
  219. * Create a HTML hidden input element.
  220. *
  221. * @param string $name
  222. * @param string $value
  223. * @param array $attributes
  224. * @return string
  225. */
  226. public static function hidden($name, $value = null, $attributes = array())
  227. {
  228. return static::input('hidden', $name, $value, $attributes);
  229. }
  230. /**
  231. * Create a HTML search input element.
  232. *
  233. * @param string $name
  234. * @param string $value
  235. * @param array $attributes
  236. * @return string
  237. */
  238. public static function search($name, $value = null, $attributes = array())
  239. {
  240. return static::input('search', $name, $value, $attributes);
  241. }
  242. /**
  243. * Create a HTML email input element.
  244. *
  245. * @param string $name
  246. * @param string $value
  247. * @param array $attributes
  248. * @return string
  249. */
  250. public static function email($name, $value = null, $attributes = array())
  251. {
  252. return static::input('email', $name, $value, $attributes);
  253. }
  254. /**
  255. * Create a HTML telephone input element.
  256. *
  257. * @param string $name
  258. * @param string $value
  259. * @param array $attributes
  260. * @return string
  261. */
  262. public static function telephone($name, $value = null, $attributes = array())
  263. {
  264. return static::input('tel', $name, $value, $attributes);
  265. }
  266. /**
  267. * Create a HTML URL input element.
  268. *
  269. * @param string $name
  270. * @param string $value
  271. * @param array $attributes
  272. * @return string
  273. */
  274. public static function url($name, $value = null, $attributes = array())
  275. {
  276. return static::input('url', $name, $value, $attributes);
  277. }
  278. /**
  279. * Create a HTML number input element.
  280. *
  281. * @param string $name
  282. * @param string $value
  283. * @param array $attributes
  284. * @return string
  285. */
  286. public static function number($name, $value = null, $attributes = array())
  287. {
  288. return static::input('number', $name, $value, $attributes);
  289. }
  290. /**
  291. * Create a HTML date input element.
  292. *
  293. * @param string $name
  294. * @param string $value
  295. * @param array $attributes
  296. * @return string
  297. */
  298. public static function date($name, $value = null, $attributes = array())
  299. {
  300. return static::input('date', $name, $value, $attributes);
  301. }
  302. /**
  303. * Create a HTML file input element.
  304. *
  305. * @param string $name
  306. * @param array $attributes
  307. * @return string
  308. */
  309. public static function file($name, $attributes = array())
  310. {
  311. return static::input('file', $name, null, $attributes);
  312. }
  313. /**
  314. * Create a HTML textarea element.
  315. *
  316. * @param string $name
  317. * @param string $value
  318. * @param array $attributes
  319. * @return string
  320. */
  321. public static function textarea($name, $value = '', $attributes = array())
  322. {
  323. $attributes['name'] = $name;
  324. $attributes['id'] = static::id($name, $attributes);
  325. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  326. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  327. return '<textarea'.HTML::attributes($attributes).'>'.HTML::entities($value).'</textarea>';
  328. }
  329. /**
  330. * Create a HTML select element.
  331. *
  332. * <code>
  333. * // Create a HTML select element filled with options
  334. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'));
  335. *
  336. * // Create a select element with a default selected value
  337. * echo Form::select('sizes', array('S' => 'Small', 'L' => 'Large'), 'L');
  338. * </code>
  339. *
  340. * @param string $name
  341. * @param array $options
  342. * @param string $selected
  343. * @param array $attributes
  344. * @return string
  345. */
  346. public static function select($name, $options = array(), $selected = null, $attributes = array())
  347. {
  348. $attributes['id'] = static::id($name, $attributes);
  349. $attributes['name'] = $name;
  350. $html = array();
  351. foreach ($options as $value => $display)
  352. {
  353. if (is_array($display))
  354. {
  355. $html[] = static::optgroup($display, $value, $selected);
  356. }
  357. else
  358. {
  359. $html[] = static::option($value, $display, $selected);
  360. }
  361. }
  362. return '<select'.HTML::attributes($attributes).'>'.implode('', $html).'</select>';
  363. }
  364. /**
  365. * Create a HTML select element optgroup.
  366. *
  367. * @param array $options
  368. * @param string $label
  369. * @param string $selected
  370. * @return string
  371. */
  372. protected static function optgroup($options, $label, $selected)
  373. {
  374. $html = array();
  375. foreach ($options as $value => $display)
  376. {
  377. $html[] = static::option($value, $display, $selected);
  378. }
  379. return '<optgroup label="'.HTML::entities($label).'">'.implode('', $html).'</optgroup>';
  380. }
  381. /**
  382. * Create a HTML select element option.
  383. *
  384. * @param string $value
  385. * @param string $display
  386. * @param string $selected
  387. * @return string
  388. */
  389. protected static function option($value, $display, $selected)
  390. {
  391. if (is_array($selected))
  392. {
  393. $selected = (in_array($value, $selected)) ? 'selected' : null;
  394. }
  395. else
  396. {
  397. $selected = ((string) $value == (string) $selected) ? 'selected' : null;
  398. }
  399. $attributes = array('value' => HTML::entities($value), 'selected' => $selected);
  400. return '<option'.HTML::attributes($attributes).'>'.HTML::entities($display).'</option>';
  401. }
  402. /**
  403. * Create a HTML checkbox input element.
  404. *
  405. * <code>
  406. * // Create a checkbox element
  407. * echo Form::checkbox('terms', 'yes');
  408. *
  409. * // Create a checkbox that is selected by default
  410. * echo Form::checkbox('terms', 'yes', true);
  411. * </code>
  412. *
  413. * @param string $name
  414. * @param string $value
  415. * @param bool $checked
  416. * @param array $attributes
  417. * @return string
  418. */
  419. public static function checkbox($name, $value = 1, $checked = false, $attributes = array())
  420. {
  421. return static::checkable('checkbox', $name, $value, $checked, $attributes);
  422. }
  423. /**
  424. * Create a HTML radio button input element.
  425. *
  426. * <code>
  427. * // Create a radio button element
  428. * echo Form::radio('drinks', 'Milk');
  429. *
  430. * // Create a radio button that is selected by default
  431. * echo Form::radio('drinks', 'Milk', true);
  432. * </code>
  433. *
  434. * @param string $name
  435. * @param string $value
  436. * @param bool $checked
  437. * @param array $attributes
  438. * @return string
  439. */
  440. public static function radio($name, $value = null, $checked = false, $attributes = array())
  441. {
  442. if (is_null($value)) $value = $name;
  443. return static::checkable('radio', $name, $value, $checked, $attributes);
  444. }
  445. /**
  446. * Create a checkable input element.
  447. *
  448. * @param string $type
  449. * @param string $name
  450. * @param string $value
  451. * @param bool $checked
  452. * @param array $attributes
  453. * @return string
  454. */
  455. protected static function checkable($type, $name, $value, $checked, $attributes)
  456. {
  457. if ($checked) $attributes['checked'] = 'checked';
  458. $attributes['id'] = static::id($name, $attributes);
  459. return static::input($type, $name, $value, $attributes);
  460. }
  461. /**
  462. * Create a HTML submit input element.
  463. *
  464. * @param string $value
  465. * @param array $attributes
  466. * @return string
  467. */
  468. public static function submit($value, $attributes = array())
  469. {
  470. return static::input('submit', null, $value, $attributes);
  471. }
  472. /**
  473. * Create a HTML reset input element.
  474. *
  475. * @param string $value
  476. * @param array $attributes
  477. * @return string
  478. */
  479. public static function reset($value, $attributes = array())
  480. {
  481. return static::input('reset', null, $value, $attributes);
  482. }
  483. /**
  484. * Create a HTML image input element.
  485. *
  486. * <code>
  487. * // Create an image input element
  488. * echo Form::image('img/submit.png');
  489. * </code>
  490. *
  491. * @param string $url
  492. * @param string $name
  493. * @param array $attributes
  494. * @return string
  495. */
  496. public static function image($url, $name = null, $attributes = array())
  497. {
  498. $attributes['src'] = URL::to_asset($url);
  499. return static::input('image', $name, null, $attributes);
  500. }
  501. /**
  502. * Create a HTML button element.
  503. *
  504. * @param string $value
  505. * @param array $attributes
  506. * @return string
  507. */
  508. public static function button($value, $attributes = array())
  509. {
  510. return '<button'.HTML::attributes($attributes).'>'.HTML::entities($value).'</button>';
  511. }
  512. /**
  513. * Determine the ID attribute for a form element.
  514. *
  515. * @param string $name
  516. * @param array $attributes
  517. * @return mixed
  518. */
  519. protected static function id($name, $attributes)
  520. {
  521. // If an ID has been explicitly specified in the attributes, we will
  522. // use that ID. Otherwise, we will look for an ID in the array of
  523. // label names so labels and their elements have the same ID.
  524. if (array_key_exists('id', $attributes))
  525. {
  526. return $attributes['id'];
  527. }
  528. if (in_array($name, static::$labels))
  529. {
  530. return $name;
  531. }
  532. }
  533. /**
  534. * Dynamically handle calls to custom macros.
  535. *
  536. * @param string $method
  537. * @param array $parameters
  538. * @return mixed
  539. */
  540. public static function __callStatic($method, $parameters)
  541. {
  542. if (isset(static::$macros[$method]))
  543. {
  544. return call_user_func_array(static::$macros[$method], $parameters);
  545. }
  546. throw new \Exception("Method [$method] does not exist.");
  547. }
  548. }