form.php 12 KB

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