form.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. <?php namespace Laravel;
  2. class Form_Facade extends Facade {
  3. public static $resolve = 'form';
  4. }
  5. class Form {
  6. /**
  7. * The request instance.
  8. *
  9. * @var Request
  10. */
  11. private $request;
  12. /**
  13. * The HTML writer instance.
  14. *
  15. * @var HTML
  16. */
  17. private $html;
  18. /**
  19. * The URL generator instance.
  20. *
  21. * @var URL
  22. */
  23. private $url;
  24. /**
  25. * All of the label names that have been created.
  26. *
  27. * These names are stored so that input elements can automatically be assigned
  28. * an ID based on the corresponding label name.
  29. *
  30. * @var array
  31. */
  32. private $labels = array();
  33. /**
  34. * Create a new form writer instance.
  35. *
  36. * @param Request $request
  37. * @param HTML $html
  38. * @param URL $url
  39. * @return void
  40. */
  41. public function __construct(Request $request, HTML $html, URL $url)
  42. {
  43. $this->url = $url;
  44. $this->html = $html;
  45. $this->request = $request;
  46. }
  47. /**
  48. * Open a HTML form.
  49. *
  50. * Note: If PUT or DELETE is specified as the form method, a hidden input field will be generated
  51. * containing the request method. PUT and DELETE are not supported by HTML forms, so the
  52. * hidden field will allow us to "spoof" PUT and DELETE requests.
  53. *
  54. * @param string $action
  55. * @param string $method
  56. * @param array $attributes
  57. * @param bool $https
  58. * @return string
  59. */
  60. public function open($action = null, $method = 'POST', $attributes = array(), $https = false)
  61. {
  62. list($attributes['action'], $attributes['method']) = array($this->action($action, $https), $this->method($method));
  63. if ( ! array_key_exists('accept-charset', $attributes))
  64. {
  65. $attributes['accept-charset'] = Config::get('application.encoding');
  66. }
  67. $append = ($method == 'PUT' or $method == 'DELETE') ? $this->hidden('REQUEST_METHOD', $method) : '';
  68. return '<form'.$this->html->attributes($attributes).'>'.$append.PHP_EOL;
  69. }
  70. /**
  71. * Determine the appropriate request method to use for a form.
  72. *
  73. * Since PUT and DELETE requests are spoofed using POST requests, we will substitute
  74. * POST for any PUT or DELETE methods. Otherwise, the specified method will be used.
  75. *
  76. * @param string $method
  77. * @return string
  78. */
  79. private function method($method)
  80. {
  81. return strtoupper(($method == 'PUT' or $method == 'DELETE') ? 'POST' : $method);
  82. }
  83. /**
  84. * Determine the appropriate action parameter to use for a form.
  85. *
  86. * If no action is specified, the current request URI will be used.
  87. *
  88. * @param string $action
  89. * @param bool $https
  90. * @return string
  91. */
  92. private function action($action, $https)
  93. {
  94. return $this->html->entities($this->url->to(((is_null($action)) ? $this->request->uri() : $action), $https));
  95. }
  96. /**
  97. * Open a HTML form with a HTTPS action URI.
  98. *
  99. * @param string $action
  100. * @param string $method
  101. * @param array $attributes
  102. * @return string
  103. */
  104. public function open_secure($action = null, $method = 'POST', $attributes = array())
  105. {
  106. return $this->open($action, $method, $attributes, true);
  107. }
  108. /**
  109. * Open a HTML form that accepts file uploads.
  110. *
  111. * @param string $action
  112. * @param string $method
  113. * @param array $attributes
  114. * @param bool $https
  115. * @return string
  116. */
  117. public function open_for_files($action = null, $method = 'POST', $attributes = array(), $https = false)
  118. {
  119. $attributes['enctype'] = 'multipart/form-data';
  120. return $this->open($action, $method, $attributes, $https);
  121. }
  122. /**
  123. * Open a HTML form that accepts file uploads with a HTTPS action URI.
  124. *
  125. * @param string $action
  126. * @param string $method
  127. * @param array $attributes
  128. * @return string
  129. */
  130. public function open_secure_for_files($action = null, $method = 'POST', $attributes = array())
  131. {
  132. return $this->open_for_files($action, $method, $attributes, true);
  133. }
  134. /**
  135. * Close a HTML form.
  136. *
  137. * @return string
  138. */
  139. public function close()
  140. {
  141. return '</form>';
  142. }
  143. /**
  144. * Generate a hidden field containing the current CSRF token.
  145. *
  146. * @return string
  147. */
  148. public function token()
  149. {
  150. return $this->input('hidden', 'csrf_token', $this->raw_token());
  151. }
  152. /**
  153. * Get the CSRF token for the current session.
  154. *
  155. * @return string
  156. */
  157. public function raw_token()
  158. {
  159. return IoC::container()->resolve('laravel.session')->get('csrf_token');
  160. }
  161. /**
  162. * Create a HTML label element.
  163. *
  164. * @param string $name
  165. * @param string $value
  166. * @param array $attributes
  167. * @return string
  168. */
  169. public function label($name, $value, $attributes = array())
  170. {
  171. $this->labels[] = $name;
  172. return '<label for="'.$name.'"'.$this->html->attributes($attributes).'>'.$this->html->entities($value).'</label>'.PHP_EOL;
  173. }
  174. /**
  175. * Create a HTML input element.
  176. *
  177. * If an ID attribute is not specified and a label has been generated matching the input
  178. * element name, the label name will be used as the element ID.
  179. *
  180. * @param string $name
  181. * @param mixed $value
  182. * @param array $attributes
  183. * @return string
  184. */
  185. public function input($type, $name, $value = null, $attributes = array())
  186. {
  187. $id = $this->id($name, $attributes);
  188. return '<input'.$this->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 function text($name, $value = null, $attributes = array())
  199. {
  200. return $this->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 function password($name, $attributes = array())
  210. {
  211. return $this->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 function hidden($name, $value = null, $attributes = array())
  222. {
  223. return $this->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 function search($name, $value = null, $attributes = array())
  234. {
  235. return $this->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 function email($name, $value = null, $attributes = array())
  246. {
  247. return $this->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 function telephone($name, $value = null, $attributes = array())
  258. {
  259. return $this->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 function url($name, $value = null, $attributes = array())
  270. {
  271. return $this->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 function number($name, $value = null, $attributes = array())
  282. {
  283. return $this->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 function file($name, $attributes = array())
  293. {
  294. return $this->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 function textarea($name, $value = '', $attributes = array())
  305. {
  306. $attributes = array_merge($attributes, array('id' => $this->id($name, $attributes), 'name' => $name));
  307. if ( ! isset($attributes['rows'])) $attributes['rows'] = 10;
  308. if ( ! isset($attributes['cols'])) $attributes['cols'] = 50;
  309. return '<textarea'.$this->html->attributes($attributes).'>'.$this->html->entities($value).'</textarea>'.PHP_EOL;
  310. }
  311. /**
  312. * Create a HTML select element.
  313. *
  314. * @param string $name
  315. * @param array $options
  316. * @param string $selected
  317. * @param array $attributes
  318. * @return string
  319. */
  320. public function select($name, $options = array(), $selected = null, $attributes = array())
  321. {
  322. $attributes = array_merge($attributes, array('id' => $this->id($name, $attributes), 'name' => $name));
  323. $html = array();
  324. foreach ($options as $value => $display)
  325. {
  326. $option_attributes = array('value' => $this->html->entities($value), 'selected' => ($value == $selected) ? 'selected' : null);
  327. $html[] = '<option'.$this->html->attributes($option_attributes).'>'.$this->html->entities($display).'</option>';
  328. }
  329. return '<select'.$this->html->attributes($attributes).'>'.implode('', $html).'</select>'.PHP_EOL;
  330. }
  331. /**
  332. * Create a HTML checkbox input element.
  333. *
  334. * @param string $name
  335. * @param string $value
  336. * @param bool $checked
  337. * @param array $attributes
  338. * @return string
  339. */
  340. public function checkbox($name, $value = null, $checked = false, $attributes = array())
  341. {
  342. return $this->checkable('checkbox', $name, $value, $checked, $attributes);
  343. }
  344. /**
  345. * Create a HTML radio button input element.
  346. *
  347. * @param string $name
  348. * @param string $value
  349. * @param bool $checked
  350. * @param array $attributes
  351. * @return string
  352. */
  353. public function radio($name, $value = null, $checked = false, $attributes = array())
  354. {
  355. return $this->checkable('radio', $name, $value, $checked, $attributes);
  356. }
  357. /**
  358. * Create a checkable input element.
  359. *
  360. * @param string $type
  361. * @param string $name
  362. * @param string $value
  363. * @param bool $checked
  364. * @param array $attributes
  365. * @return string
  366. */
  367. private function checkable($type, $name, $value, $checked, $attributes)
  368. {
  369. $attributes = array_merge($attributes, array('id' => $this->id($name, $attributes), 'checked' => ($checked) ? 'checked' : null));
  370. return $this->input($type, $name, $value, $attributes);
  371. }
  372. /**
  373. * Create a HTML submit input element.
  374. *
  375. * @param string $value
  376. * @param array $attributes
  377. * @return string
  378. */
  379. public function submit($value, $attributes = array())
  380. {
  381. return $this->input('submit', null, $value, $attributes);
  382. }
  383. /**
  384. * Create a HTML reset input element.
  385. *
  386. * @param string $value
  387. * @param array $attributes
  388. * @return string
  389. */
  390. public function reset($value, $attributes = array())
  391. {
  392. return $this->input('reset', null, $value, $attributes);
  393. }
  394. /**
  395. * Create a HTML image input element.
  396. *
  397. * @param string $url
  398. * @param array $attributes
  399. * @return string
  400. */
  401. public function image($url, $name = null, $attributes = array())
  402. {
  403. $attributes['src'] = $this->url->to_asset($url);
  404. return $this->input('image', $name, null, $attributes);
  405. }
  406. /**
  407. * Create a HTML button element.
  408. *
  409. * @param string $name
  410. * @param string $value
  411. * @param array $attributes
  412. * @return string
  413. */
  414. public function button($value, $attributes = array())
  415. {
  416. return '<button'.$this->html->attributes($attributes).'>'.$this->html->entities($value).'</button>'.PHP_EOL;
  417. }
  418. /**
  419. * Determine the ID attribute for a form element.
  420. *
  421. * An explicitly specified ID in the attributes takes first precedence, then
  422. * the label names will be checked for a label matching the element name.
  423. *
  424. * @param string $name
  425. * @param array $attributes
  426. * @return mixed
  427. */
  428. private function id($name, $attributes)
  429. {
  430. if (array_key_exists('id', $attributes)) return $attributes['id'];
  431. if (in_array($name, $this->labels)) return $name;
  432. }
  433. }