form_helper.php 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Form Helpers
  41. *
  42. * @package CodeIgniter
  43. * @subpackage Helpers
  44. * @category Helpers
  45. * @author EllisLab Dev Team
  46. * @link https://codeigniter.com/user_guide/helpers/form_helper.html
  47. */
  48. // ------------------------------------------------------------------------
  49. if ( ! function_exists('form_open'))
  50. {
  51. /**
  52. * Form Declaration
  53. *
  54. * Creates the opening portion of the form.
  55. *
  56. * @param string the URI segments of the form destination
  57. * @param array a key/value pair of attributes
  58. * @param array a key/value pair hidden data
  59. * @return string
  60. */
  61. function form_open($action = '', $attributes = array(), $hidden = array())
  62. {
  63. $CI =& get_instance();
  64. // If no action is provided then set to the current url
  65. if ( ! $action)
  66. {
  67. $action = $CI->config->site_url($CI->uri->uri_string());
  68. }
  69. // If an action is not a full URL then turn it into one
  70. elseif (strpos($action, '://') === FALSE)
  71. {
  72. $action = $CI->config->site_url($action);
  73. }
  74. $attributes = _attributes_to_string($attributes);
  75. if (stripos($attributes, 'method=') === FALSE)
  76. {
  77. $attributes .= ' method="post"';
  78. }
  79. if (stripos($attributes, 'accept-charset=') === FALSE)
  80. {
  81. $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
  82. }
  83. $form = '<form action="'.$action.'"'.$attributes.">\n";
  84. if (is_array($hidden))
  85. {
  86. foreach ($hidden as $name => $value)
  87. {
  88. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value).'" />'."\n";
  89. }
  90. }
  91. // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites
  92. if ($CI->config->item('csrf_protection') === TRUE && strpos($action, $CI->config->base_url()) !== FALSE && ! stripos($form, 'method="get"'))
  93. {
  94. // Prepend/append random-length "white noise" around the CSRF
  95. // token input, as a form of protection against BREACH attacks
  96. if (FALSE !== ($noise = $CI->security->get_random_bytes(1)))
  97. {
  98. list(, $noise) = unpack('c', $noise);
  99. }
  100. else
  101. {
  102. $noise = mt_rand(-128, 127);
  103. }
  104. // Prepend if $noise has a negative value, append if positive, do nothing for zero
  105. $prepend = $append = '';
  106. if ($noise < 0)
  107. {
  108. $prepend = str_repeat(" ", abs($noise));
  109. }
  110. elseif ($noise > 0)
  111. {
  112. $append = str_repeat(" ", $noise);
  113. }
  114. $form .= sprintf(
  115. '%s<input type="hidden" name="%s" value="%s" />%s%s',
  116. $prepend,
  117. $CI->security->get_csrf_token_name(),
  118. $CI->security->get_csrf_hash(),
  119. $append,
  120. "\n"
  121. );
  122. }
  123. return $form;
  124. }
  125. }
  126. // ------------------------------------------------------------------------
  127. if ( ! function_exists('form_open_multipart'))
  128. {
  129. /**
  130. * Form Declaration - Multipart type
  131. *
  132. * Creates the opening portion of the form, but with "multipart/form-data".
  133. *
  134. * @param string the URI segments of the form destination
  135. * @param array a key/value pair of attributes
  136. * @param array a key/value pair hidden data
  137. * @return string
  138. */
  139. function form_open_multipart($action = '', $attributes = array(), $hidden = array())
  140. {
  141. if (is_string($attributes))
  142. {
  143. $attributes .= ' enctype="multipart/form-data"';
  144. }
  145. else
  146. {
  147. $attributes['enctype'] = 'multipart/form-data';
  148. }
  149. return form_open($action, $attributes, $hidden);
  150. }
  151. }
  152. // ------------------------------------------------------------------------
  153. if ( ! function_exists('form_hidden'))
  154. {
  155. /**
  156. * Hidden Input Field
  157. *
  158. * Generates hidden fields. You can pass a simple key/value string or
  159. * an associative array with multiple values.
  160. *
  161. * @param mixed $name Field name
  162. * @param string $value Field value
  163. * @param bool $recursing
  164. * @return string
  165. */
  166. function form_hidden($name, $value = '', $recursing = FALSE)
  167. {
  168. static $form;
  169. if ($recursing === FALSE)
  170. {
  171. $form = "\n";
  172. }
  173. if (is_array($name))
  174. {
  175. foreach ($name as $key => $val)
  176. {
  177. form_hidden($key, $val, TRUE);
  178. }
  179. return $form;
  180. }
  181. if ( ! is_array($value))
  182. {
  183. $form .= '<input type="hidden" name="'.$name.'" value="'.html_escape($value)."\" />\n";
  184. }
  185. else
  186. {
  187. foreach ($value as $k => $v)
  188. {
  189. $k = is_int($k) ? '' : $k;
  190. form_hidden($name.'['.$k.']', $v, TRUE);
  191. }
  192. }
  193. return $form;
  194. }
  195. }
  196. // ------------------------------------------------------------------------
  197. if ( ! function_exists('form_input'))
  198. {
  199. /**
  200. * Text Input Field
  201. *
  202. * @param mixed
  203. * @param string
  204. * @param mixed
  205. * @return string
  206. */
  207. function form_input($data = '', $value = '', $extra = '')
  208. {
  209. $defaults = array(
  210. 'type' => 'text',
  211. 'name' => is_array($data) ? '' : $data,
  212. 'value' => $value
  213. );
  214. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  215. }
  216. }
  217. // ------------------------------------------------------------------------
  218. if ( ! function_exists('form_password'))
  219. {
  220. /**
  221. * Password Field
  222. *
  223. * Identical to the input function but adds the "password" type
  224. *
  225. * @param mixed
  226. * @param string
  227. * @param mixed
  228. * @return string
  229. */
  230. function form_password($data = '', $value = '', $extra = '')
  231. {
  232. is_array($data) OR $data = array('name' => $data);
  233. $data['type'] = 'password';
  234. return form_input($data, $value, $extra);
  235. }
  236. }
  237. // ------------------------------------------------------------------------
  238. if ( ! function_exists('form_upload'))
  239. {
  240. /**
  241. * Upload Field
  242. *
  243. * Identical to the input function but adds the "file" type
  244. *
  245. * @param mixed
  246. * @param string
  247. * @param mixed
  248. * @return string
  249. */
  250. function form_upload($data = '', $value = '', $extra = '')
  251. {
  252. $defaults = array('type' => 'file', 'name' => '');
  253. is_array($data) OR $data = array('name' => $data);
  254. $data['type'] = 'file';
  255. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  256. }
  257. }
  258. // ------------------------------------------------------------------------
  259. if ( ! function_exists('form_textarea'))
  260. {
  261. /**
  262. * Textarea field
  263. *
  264. * @param mixed $data
  265. * @param string $value
  266. * @param mixed $extra
  267. * @return string
  268. */
  269. function form_textarea($data = '', $value = '', $extra = '')
  270. {
  271. $defaults = array(
  272. 'name' => is_array($data) ? '' : $data,
  273. 'cols' => '40',
  274. 'rows' => '10'
  275. );
  276. if ( ! is_array($data) OR ! isset($data['value']))
  277. {
  278. $val = $value;
  279. }
  280. else
  281. {
  282. $val = $data['value'];
  283. unset($data['value']); // textareas don't use the value attribute
  284. }
  285. return '<textarea '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  286. .html_escape($val)
  287. ."</textarea>\n";
  288. }
  289. }
  290. // ------------------------------------------------------------------------
  291. if ( ! function_exists('form_multiselect'))
  292. {
  293. /**
  294. * Multi-select menu
  295. *
  296. * @param string
  297. * @param array
  298. * @param mixed
  299. * @param mixed
  300. * @return string
  301. */
  302. function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
  303. {
  304. $extra = _attributes_to_string($extra);
  305. if (stripos($extra, 'multiple') === FALSE)
  306. {
  307. $extra .= ' multiple="multiple"';
  308. }
  309. return form_dropdown($name, $options, $selected, $extra);
  310. }
  311. }
  312. // --------------------------------------------------------------------
  313. if ( ! function_exists('form_dropdown'))
  314. {
  315. /**
  316. * Drop-down Menu
  317. *
  318. * @param mixed $data
  319. * @param mixed $options
  320. * @param mixed $selected
  321. * @param mixed $extra
  322. * @return string
  323. */
  324. function form_dropdown($data = '', $options = array(), $selected = array(), $extra = '')
  325. {
  326. $defaults = array();
  327. if (is_array($data))
  328. {
  329. if (isset($data['selected']))
  330. {
  331. $selected = $data['selected'];
  332. unset($data['selected']); // select tags don't have a selected attribute
  333. }
  334. if (isset($data['options']))
  335. {
  336. $options = $data['options'];
  337. unset($data['options']); // select tags don't use an options attribute
  338. }
  339. }
  340. else
  341. {
  342. $defaults = array('name' => $data);
  343. }
  344. is_array($selected) OR $selected = array($selected);
  345. is_array($options) OR $options = array($options);
  346. // If no selected state was submitted we will attempt to set it automatically
  347. if (empty($selected))
  348. {
  349. if (is_array($data))
  350. {
  351. if (isset($data['name'], $_POST[$data['name']]))
  352. {
  353. $selected = array($_POST[$data['name']]);
  354. }
  355. }
  356. elseif (isset($_POST[$data]))
  357. {
  358. $selected = array($_POST[$data]);
  359. }
  360. }
  361. $extra = _attributes_to_string($extra);
  362. $multiple = (count($selected) > 1 && stripos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
  363. $form = '<select '.rtrim(_parse_form_attributes($data, $defaults)).$extra.$multiple.">\n";
  364. foreach ($options as $key => $val)
  365. {
  366. $key = (string) $key;
  367. if (is_array($val))
  368. {
  369. if (empty($val))
  370. {
  371. continue;
  372. }
  373. $form .= '<optgroup label="'.$key."\">\n";
  374. foreach ($val as $optgroup_key => $optgroup_val)
  375. {
  376. $sel = in_array($optgroup_key, $selected) ? ' selected="selected"' : '';
  377. $form .= '<option value="'.html_escape($optgroup_key).'"'.$sel.'>'
  378. .(string) $optgroup_val."</option>\n";
  379. }
  380. $form .= "</optgroup>\n";
  381. }
  382. else
  383. {
  384. $form .= '<option value="'.html_escape($key).'"'
  385. .(in_array($key, $selected) ? ' selected="selected"' : '').'>'
  386. .(string) $val."</option>\n";
  387. }
  388. }
  389. return $form."</select>\n";
  390. }
  391. }
  392. // ------------------------------------------------------------------------
  393. if ( ! function_exists('form_checkbox'))
  394. {
  395. /**
  396. * Checkbox Field
  397. *
  398. * @param mixed
  399. * @param string
  400. * @param bool
  401. * @param mixed
  402. * @return string
  403. */
  404. function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
  405. {
  406. $defaults = array('type' => 'checkbox', 'name' => ( ! is_array($data) ? $data : ''), 'value' => $value);
  407. if (is_array($data) && array_key_exists('checked', $data))
  408. {
  409. $checked = $data['checked'];
  410. if ($checked == FALSE)
  411. {
  412. unset($data['checked']);
  413. }
  414. else
  415. {
  416. $data['checked'] = 'checked';
  417. }
  418. }
  419. if ($checked == TRUE)
  420. {
  421. $defaults['checked'] = 'checked';
  422. }
  423. else
  424. {
  425. unset($defaults['checked']);
  426. }
  427. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  428. }
  429. }
  430. // ------------------------------------------------------------------------
  431. if ( ! function_exists('form_radio'))
  432. {
  433. /**
  434. * Radio Button
  435. *
  436. * @param mixed
  437. * @param string
  438. * @param bool
  439. * @param mixed
  440. * @return string
  441. */
  442. function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
  443. {
  444. is_array($data) OR $data = array('name' => $data);
  445. $data['type'] = 'radio';
  446. return form_checkbox($data, $value, $checked, $extra);
  447. }
  448. }
  449. // ------------------------------------------------------------------------
  450. if ( ! function_exists('form_submit'))
  451. {
  452. /**
  453. * Submit Button
  454. *
  455. * @param mixed
  456. * @param string
  457. * @param mixed
  458. * @return string
  459. */
  460. function form_submit($data = '', $value = '', $extra = '')
  461. {
  462. $defaults = array(
  463. 'type' => 'submit',
  464. 'name' => is_array($data) ? '' : $data,
  465. 'value' => $value
  466. );
  467. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  468. }
  469. }
  470. // ------------------------------------------------------------------------
  471. if ( ! function_exists('form_reset'))
  472. {
  473. /**
  474. * Reset Button
  475. *
  476. * @param mixed
  477. * @param string
  478. * @param mixed
  479. * @return string
  480. */
  481. function form_reset($data = '', $value = '', $extra = '')
  482. {
  483. $defaults = array(
  484. 'type' => 'reset',
  485. 'name' => is_array($data) ? '' : $data,
  486. 'value' => $value
  487. );
  488. return '<input '._parse_form_attributes($data, $defaults)._attributes_to_string($extra)." />\n";
  489. }
  490. }
  491. // ------------------------------------------------------------------------
  492. if ( ! function_exists('form_button'))
  493. {
  494. /**
  495. * Form Button
  496. *
  497. * @param mixed
  498. * @param string
  499. * @param mixed
  500. * @return string
  501. */
  502. function form_button($data = '', $content = '', $extra = '')
  503. {
  504. $defaults = array(
  505. 'name' => is_array($data) ? '' : $data,
  506. 'type' => 'button'
  507. );
  508. if (is_array($data) && isset($data['content']))
  509. {
  510. $content = $data['content'];
  511. unset($data['content']); // content is not an attribute
  512. }
  513. return '<button '._parse_form_attributes($data, $defaults)._attributes_to_string($extra).'>'
  514. .$content
  515. ."</button>\n";
  516. }
  517. }
  518. // ------------------------------------------------------------------------
  519. if ( ! function_exists('form_label'))
  520. {
  521. /**
  522. * Form Label Tag
  523. *
  524. * @param string The text to appear onscreen
  525. * @param string The id the label applies to
  526. * @param array Additional attributes
  527. * @return string
  528. */
  529. function form_label($label_text = '', $id = '', $attributes = array())
  530. {
  531. $label = '<label';
  532. if ($id !== '')
  533. {
  534. $label .= ' for="'.$id.'"';
  535. }
  536. if (is_array($attributes) && count($attributes) > 0)
  537. {
  538. foreach ($attributes as $key => $val)
  539. {
  540. $label .= ' '.$key.'="'.$val.'"';
  541. }
  542. }
  543. return $label.'>'.$label_text.'</label>';
  544. }
  545. }
  546. // ------------------------------------------------------------------------
  547. if ( ! function_exists('form_fieldset'))
  548. {
  549. /**
  550. * Fieldset Tag
  551. *
  552. * Used to produce <fieldset><legend>text</legend>. To close fieldset
  553. * use form_fieldset_close()
  554. *
  555. * @param string The legend text
  556. * @param array Additional attributes
  557. * @return string
  558. */
  559. function form_fieldset($legend_text = '', $attributes = array())
  560. {
  561. $fieldset = '<fieldset'._attributes_to_string($attributes).">\n";
  562. if ($legend_text !== '')
  563. {
  564. return $fieldset.'<legend>'.$legend_text."</legend>\n";
  565. }
  566. return $fieldset;
  567. }
  568. }
  569. // ------------------------------------------------------------------------
  570. if ( ! function_exists('form_fieldset_close'))
  571. {
  572. /**
  573. * Fieldset Close Tag
  574. *
  575. * @param string
  576. * @return string
  577. */
  578. function form_fieldset_close($extra = '')
  579. {
  580. return '</fieldset>'.$extra;
  581. }
  582. }
  583. // ------------------------------------------------------------------------
  584. if ( ! function_exists('form_close'))
  585. {
  586. /**
  587. * Form Close Tag
  588. *
  589. * @param string
  590. * @return string
  591. */
  592. function form_close($extra = '')
  593. {
  594. return '</form>'.$extra;
  595. }
  596. }
  597. // ------------------------------------------------------------------------
  598. if ( ! function_exists('form_prep'))
  599. {
  600. /**
  601. * Form Prep
  602. *
  603. * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
  604. *
  605. * @deprecated 3.0.0 An alias for html_escape()
  606. * @param string|string[] $str Value to escape
  607. * @return string|string[] Escaped values
  608. */
  609. function form_prep($str)
  610. {
  611. return html_escape($str, TRUE);
  612. }
  613. }
  614. // ------------------------------------------------------------------------
  615. if ( ! function_exists('set_value'))
  616. {
  617. /**
  618. * Form Value
  619. *
  620. * Grabs a value from the POST array for the specified field so you can
  621. * re-populate an input field or textarea. If Form Validation
  622. * is active it retrieves the info from the validation class
  623. *
  624. * @param string $field Field name
  625. * @param string $default Default value
  626. * @param bool $html_escape Whether to escape HTML special characters or not
  627. * @return string
  628. */
  629. function set_value($field, $default = '', $html_escape = TRUE)
  630. {
  631. $CI =& get_instance();
  632. $value = (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  633. ? $CI->form_validation->set_value($field, $default)
  634. : $CI->input->post($field, FALSE);
  635. isset($value) OR $value = $default;
  636. return ($html_escape) ? html_escape($value) : $value;
  637. }
  638. }
  639. // ------------------------------------------------------------------------
  640. if ( ! function_exists('set_select'))
  641. {
  642. /**
  643. * Set Select
  644. *
  645. * Let's you set the selected value of a <select> menu via data in the POST array.
  646. * If Form Validation is active it retrieves the info from the validation class
  647. *
  648. * @param string
  649. * @param string
  650. * @param bool
  651. * @return string
  652. */
  653. function set_select($field, $value = '', $default = FALSE)
  654. {
  655. $CI =& get_instance();
  656. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  657. {
  658. return $CI->form_validation->set_select($field, $value, $default);
  659. }
  660. elseif (($input = $CI->input->post($field, FALSE)) === NULL)
  661. {
  662. return ($default === TRUE) ? ' selected="selected"' : '';
  663. }
  664. $value = (string) $value;
  665. if (is_array($input))
  666. {
  667. // Note: in_array('', array(0)) returns TRUE, do not use it
  668. foreach ($input as &$v)
  669. {
  670. if ($value === $v)
  671. {
  672. return ' selected="selected"';
  673. }
  674. }
  675. return '';
  676. }
  677. return ($input === $value) ? ' selected="selected"' : '';
  678. }
  679. }
  680. // ------------------------------------------------------------------------
  681. if ( ! function_exists('set_checkbox'))
  682. {
  683. /**
  684. * Set Checkbox
  685. *
  686. * Let's you set the selected value of a checkbox via the value in the POST array.
  687. * If Form Validation is active it retrieves the info from the validation class
  688. *
  689. * @param string
  690. * @param string
  691. * @param bool
  692. * @return string
  693. */
  694. function set_checkbox($field, $value = '', $default = FALSE)
  695. {
  696. $CI =& get_instance();
  697. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  698. {
  699. return $CI->form_validation->set_checkbox($field, $value, $default);
  700. }
  701. // Form inputs are always strings ...
  702. $value = (string) $value;
  703. $input = $CI->input->post($field, FALSE);
  704. if (is_array($input))
  705. {
  706. // Note: in_array('', array(0)) returns TRUE, do not use it
  707. foreach ($input as &$v)
  708. {
  709. if ($value === $v)
  710. {
  711. return ' checked="checked"';
  712. }
  713. }
  714. return '';
  715. }
  716. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  717. if ($CI->input->method() === 'post')
  718. {
  719. return ($input === $value) ? ' checked="checked"' : '';
  720. }
  721. return ($default === TRUE) ? ' checked="checked"' : '';
  722. }
  723. }
  724. // ------------------------------------------------------------------------
  725. if ( ! function_exists('set_radio'))
  726. {
  727. /**
  728. * Set Radio
  729. *
  730. * Let's you set the selected value of a radio field via info in the POST array.
  731. * If Form Validation is active it retrieves the info from the validation class
  732. *
  733. * @param string $field
  734. * @param string $value
  735. * @param bool $default
  736. * @return string
  737. */
  738. function set_radio($field, $value = '', $default = FALSE)
  739. {
  740. $CI =& get_instance();
  741. if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field))
  742. {
  743. return $CI->form_validation->set_radio($field, $value, $default);
  744. }
  745. // Form inputs are always strings ...
  746. $value = (string) $value;
  747. $input = $CI->input->post($field, FALSE);
  748. if (is_array($input))
  749. {
  750. // Note: in_array('', array(0)) returns TRUE, do not use it
  751. foreach ($input as &$v)
  752. {
  753. if ($value === $v)
  754. {
  755. return ' checked="checked"';
  756. }
  757. }
  758. return '';
  759. }
  760. // Unchecked checkbox and radio inputs are not even submitted by browsers ...
  761. if ($CI->input->method() === 'post')
  762. {
  763. return ($input === $value) ? ' checked="checked"' : '';
  764. }
  765. return ($default === TRUE) ? ' checked="checked"' : '';
  766. }
  767. }
  768. // ------------------------------------------------------------------------
  769. if ( ! function_exists('form_error'))
  770. {
  771. /**
  772. * Form Error
  773. *
  774. * Returns the error for a specific form field. This is a helper for the
  775. * form validation class.
  776. *
  777. * @param string
  778. * @param string
  779. * @param string
  780. * @return string
  781. */
  782. function form_error($field = '', $prefix = '', $suffix = '')
  783. {
  784. if (FALSE === ($OBJ =& _get_validation_object()))
  785. {
  786. return '';
  787. }
  788. return $OBJ->error($field, $prefix, $suffix);
  789. }
  790. }
  791. // ------------------------------------------------------------------------
  792. if ( ! function_exists('validation_errors'))
  793. {
  794. /**
  795. * Validation Error String
  796. *
  797. * Returns all the errors associated with a form submission. This is a helper
  798. * function for the form validation class.
  799. *
  800. * @param string
  801. * @param string
  802. * @return string
  803. */
  804. function validation_errors($prefix = '', $suffix = '')
  805. {
  806. if (FALSE === ($OBJ =& _get_validation_object()))
  807. {
  808. return '';
  809. }
  810. return $OBJ->error_string($prefix, $suffix);
  811. }
  812. }
  813. // ------------------------------------------------------------------------
  814. if ( ! function_exists('_parse_form_attributes'))
  815. {
  816. /**
  817. * Parse the form attributes
  818. *
  819. * Helper function used by some of the form helpers
  820. *
  821. * @param array $attributes List of attributes
  822. * @param array $default Default values
  823. * @return string
  824. */
  825. function _parse_form_attributes($attributes, $default)
  826. {
  827. if (is_array($attributes))
  828. {
  829. foreach ($default as $key => $val)
  830. {
  831. if (isset($attributes[$key]))
  832. {
  833. $default[$key] = $attributes[$key];
  834. unset($attributes[$key]);
  835. }
  836. }
  837. if (count($attributes) > 0)
  838. {
  839. $default = array_merge($default, $attributes);
  840. }
  841. }
  842. $att = '';
  843. foreach ($default as $key => $val)
  844. {
  845. if ($key === 'value')
  846. {
  847. $val = html_escape($val);
  848. }
  849. elseif ($key === 'name' && ! strlen($default['name']))
  850. {
  851. continue;
  852. }
  853. $att .= $key.'="'.$val.'" ';
  854. }
  855. return $att;
  856. }
  857. }
  858. // ------------------------------------------------------------------------
  859. if ( ! function_exists('_attributes_to_string'))
  860. {
  861. /**
  862. * Attributes To String
  863. *
  864. * Helper function used by some of the form helpers
  865. *
  866. * @param mixed
  867. * @return string
  868. */
  869. function _attributes_to_string($attributes)
  870. {
  871. if (empty($attributes))
  872. {
  873. return '';
  874. }
  875. if (is_object($attributes))
  876. {
  877. $attributes = (array) $attributes;
  878. }
  879. if (is_array($attributes))
  880. {
  881. $atts = '';
  882. foreach ($attributes as $key => $val)
  883. {
  884. $atts .= ' '.$key.'="'.$val.'"';
  885. }
  886. return $atts;
  887. }
  888. if (is_string($attributes))
  889. {
  890. return ' '.$attributes;
  891. }
  892. return FALSE;
  893. }
  894. }
  895. // ------------------------------------------------------------------------
  896. if ( ! function_exists('_get_validation_object'))
  897. {
  898. /**
  899. * Validation Object
  900. *
  901. * Determines what the form validation class was instantiated as, fetches
  902. * the object and returns it.
  903. *
  904. * @return mixed
  905. */
  906. function &_get_validation_object()
  907. {
  908. $CI =& get_instance();
  909. // We set this as a variable since we're returning by reference.
  910. $return = FALSE;
  911. if (FALSE !== ($object = $CI->load->is_loaded('Form_validation')))
  912. {
  913. if ( ! isset($CI->$object) OR ! is_object($CI->$object))
  914. {
  915. return $return;
  916. }
  917. return $CI->$object;
  918. }
  919. return $return;
  920. }
  921. }