form.php 12 KB

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