form_helper.php 21 KB

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