form_helper.php 21 KB

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