form.php 12 KB

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