1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2009, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Form Helpers
21 * @package CodeIgniter
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/helpers/form_helper.html
28 // ------------------------------------------------------------------------
33 * Creates the opening portion of the form.
36 * @param string the URI segments of the form destination
37 * @param array a key/value pair of attributes
38 * @param array a key/value pair hidden data
41 if ( ! function_exists('form_open'))
43 function form_open($action = '', $attributes = '', $hidden = array())
45 $CI =& get_instance();
47 if ($attributes == '')
49 $attributes = 'method="post"';
52 $action = ( strpos($action, '://') === FALSE) ? $CI->config->site_url($action) : $action;
54 $form = '<form action="'.$action.'"';
56 $form .= _attributes_to_string($attributes, TRUE);
60 if (is_array($hidden) AND count($hidden) > 0)
62 $form .= form_hidden($hidden);
69 // ------------------------------------------------------------------------
72 * Form Declaration - Multipart type
74 * Creates the opening portion of the form, but with "multipart/form-data".
77 * @param string the URI segments of the form destination
78 * @param array a key/value pair of attributes
79 * @param array a key/value pair hidden data
82 if ( ! function_exists('form_open_multipart'))
84 function form_open_multipart($action, $attributes = array(), $hidden = array())
86 $attributes['enctype'] = 'multipart/form-data';
87 return form_open($action, $attributes, $hidden);
91 // ------------------------------------------------------------------------
96 * Generates hidden fields. You can pass a simple key/value string or an associative
97 * array with multiple values.
104 if ( ! function_exists('form_hidden'))
106 function form_hidden($name, $value = '', $recursing = FALSE)
110 if ($recursing === FALSE)
117 foreach ($name as $key => $val)
119 form_hidden($key, $val, TRUE);
124 if ( ! is_array($value))
126 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
130 foreach ($value as $k => $v)
132 $k = (is_int($k)) ? '' : $k;
133 form_hidden($name.'['.$k.']', $v, TRUE);
141 // ------------------------------------------------------------------------
152 if ( ! function_exists('form_input'))
154 function form_input($data = '', $value = '', $extra = '')
156 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
158 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
162 // ------------------------------------------------------------------------
167 * Identical to the input function but adds the "password" type
175 if ( ! function_exists('form_password'))
177 function form_password($data = '', $value = '', $extra = '')
179 if ( ! is_array($data))
181 $data = array('name' => $data);
184 $data['type'] = 'password';
185 return form_input($data, $value, $extra);
189 // ------------------------------------------------------------------------
194 * Identical to the input function but adds the "file" type
202 if ( ! function_exists('form_upload'))
204 function form_upload($data = '', $value = '', $extra = '')
206 if ( ! is_array($data))
208 $data = array('name' => $data);
211 $data['type'] = 'file';
212 return form_input($data, $value, $extra);
216 // ------------------------------------------------------------------------
227 if ( ! function_exists('form_textarea'))
229 function form_textarea($data = '', $value = '', $extra = '')
231 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
233 if ( ! is_array($data) OR ! isset($data['value']))
239 $val = $data['value'];
240 unset($data['value']); // textareas don't use the value attribute
243 $name = (is_array($data)) ? $data['name'] : $data;
244 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
248 // ------------------------------------------------------------------------
260 if (! function_exists('form_multiselect'))
262 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
264 if ( ! strpos($extra, 'multiple'))
266 $extra .= ' multiple="multiple"';
269 return form_dropdown($name, $options, $selected, $extra);
273 // --------------------------------------------------------------------
285 if ( ! function_exists('form_dropdown'))
287 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
289 if ( ! is_array($selected))
291 $selected = array($selected);
294 // If no selected state was submitted we will attempt to set it automatically
295 if (count($selected) === 0)
297 // If the form name appears in the $_POST array we have a winner!
298 if (isset($_POST[$name]))
300 $selected = array($_POST[$name]);
304 if ($extra != '') $extra = ' '.$extra;
306 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
308 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
310 foreach ($options as $key => $val)
312 $key = (string) $key;
316 $form .= '<optgroup label="'.$key.'">'."\n";
318 foreach ($val as $optgroup_key => $optgroup_val)
320 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
322 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
325 $form .= '</optgroup>'."\n";
329 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
331 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
335 $form .= '</select>';
341 // ------------------------------------------------------------------------
353 if ( ! function_exists('form_checkbox'))
355 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
357 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
359 if (is_array($data) AND array_key_exists('checked', $data))
361 $checked = $data['checked'];
363 if ($checked == FALSE)
365 unset($data['checked']);
369 $data['checked'] = 'checked';
373 if ($checked == TRUE)
375 $defaults['checked'] = 'checked';
379 unset($defaults['checked']);
382 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
386 // ------------------------------------------------------------------------
398 if ( ! function_exists('form_radio'))
400 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
402 if ( ! is_array($data))
404 $data = array('name' => $data);
407 $data['type'] = 'radio';
408 return form_checkbox($data, $value, $checked, $extra);
412 // ------------------------------------------------------------------------
423 if ( ! function_exists('form_submit'))
425 function form_submit($data = '', $value = '', $extra = '')
427 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
429 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
433 // ------------------------------------------------------------------------
444 if ( ! function_exists('form_reset'))
446 function form_reset($data = '', $value = '', $extra = '')
448 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
450 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
454 // ------------------------------------------------------------------------
465 if ( ! function_exists('form_button'))
467 function form_button($data = '', $content = '', $extra = '')
469 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
471 if ( is_array($data) AND isset($data['content']))
473 $content = $data['content'];
474 unset($data['content']); // content is not an attribute
477 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
481 // ------------------------------------------------------------------------
487 * @param string The text to appear onscreen
488 * @param string The id the label applies to
489 * @param string Additional attributes
492 if ( ! function_exists('form_label'))
494 function form_label($label_text = '', $id = '', $attributes = array())
501 $label .= " for=\"$id\"";
504 if (is_array($attributes) AND count($attributes) > 0)
506 foreach ($attributes as $key => $val)
508 $label .= ' '.$key.'="'.$val.'"';
512 $label .= ">$label_text</label>";
518 // ------------------------------------------------------------------------
522 * Used to produce <fieldset><legend>text</legend>. To close fieldset
523 * use form_fieldset_close()
526 * @param string The legend text
527 * @param string Additional attributes
530 if ( ! function_exists('form_fieldset'))
532 function form_fieldset($legend_text = '', $attributes = array())
534 $fieldset = "<fieldset";
536 $fieldset .= _attributes_to_string($attributes, FALSE);
540 if ($legend_text != '')
542 $fieldset .= "<legend>$legend_text</legend>\n";
549 // ------------------------------------------------------------------------
558 if ( ! function_exists('form_fieldset_close'))
560 function form_fieldset_close($extra = '')
562 return "</fieldset>".$extra;
566 // ------------------------------------------------------------------------
575 if ( ! function_exists('form_close'))
577 function form_close($extra = '')
579 return "</form>".$extra;
583 // ------------------------------------------------------------------------
588 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
594 if ( ! function_exists('form_prep'))
596 function form_prep($str = '', $field_name = '')
598 static $prepped_fields = array();
600 // if the field name is an array we do this recursively
603 foreach ($str as $key => $val)
605 $str[$key] = form_prep($val);
616 // we've already prepped a field with this name
617 // @todo need to figure out a way to namespace this so
618 // that we know the *exact* field and not just one with
620 if (isset($prepped_fields[$field_name]))
625 $str = htmlspecialchars($str);
627 // In case htmlspecialchars misses these.
628 $str = str_replace(array("'", '"'), array("'", """), $str);
630 if ($field_name != '')
632 $prepped_fields[$field_name] = $str;
639 // ------------------------------------------------------------------------
644 * Grabs a value from the POST array for the specified field so you can
645 * re-populate an input field or textarea. If Form Validation
646 * is active it retrieves the info from the validation class
652 if ( ! function_exists('set_value'))
654 function set_value($field = '', $default = '')
656 if (FALSE === ($OBJ =& _get_validation_object()))
658 if ( ! isset($_POST[$field]))
663 return form_prep($_POST[$field], $field);
666 return form_prep($OBJ->set_value($field, $default), $field);
670 // ------------------------------------------------------------------------
675 * Let's you set the selected value of a <select> menu via data in the POST array.
676 * If Form Validation is active it retrieves the info from the validation class
684 if ( ! function_exists('set_select'))
686 function set_select($field = '', $value = '', $default = FALSE)
688 $OBJ =& _get_validation_object();
692 if ( ! isset($_POST[$field]))
694 if (count($_POST) === 0 AND $default == TRUE)
696 return ' selected="selected"';
701 $field = $_POST[$field];
703 if (is_array($field))
705 if ( ! in_array($value, $field))
712 if (($field == '' OR $value == '') OR ($field != $value))
718 return ' selected="selected"';
721 return $OBJ->set_select($field, $value, $default);
725 // ------------------------------------------------------------------------
730 * Let's you set the selected value of a checkbox via the value in the POST array.
731 * If Form Validation is active it retrieves the info from the validation class
739 if ( ! function_exists('set_checkbox'))
741 function set_checkbox($field = '', $value = '', $default = FALSE)
743 $OBJ =& _get_validation_object();
747 if ( ! isset($_POST[$field]))
749 if (count($_POST) === 0 AND $default == TRUE)
751 return ' checked="checked"';
756 $field = $_POST[$field];
758 if (is_array($field))
760 if ( ! in_array($value, $field))
767 if (($field == '' OR $value == '') OR ($field != $value))
773 return ' checked="checked"';
776 return $OBJ->set_checkbox($field, $value, $default);
780 // ------------------------------------------------------------------------
785 * Let's you set the selected value of a radio field via info in the POST array.
786 * If Form Validation is active it retrieves the info from the validation class
794 if ( ! function_exists('set_radio'))
796 function set_radio($field = '', $value = '', $default = FALSE)
798 $OBJ =& _get_validation_object();
802 if ( ! isset($_POST[$field]))
804 if (count($_POST) === 0 AND $default == TRUE)
806 return ' checked="checked"';
811 $field = $_POST[$field];
813 if (is_array($field))
815 if ( ! in_array($value, $field))
822 if (($field == '' OR $value == '') OR ($field != $value))
828 return ' checked="checked"';
831 return $OBJ->set_radio($field, $value, $default);
835 // ------------------------------------------------------------------------
840 * Returns the error for a specific form field. This is a helper for the
841 * form validation class.
849 if ( ! function_exists('form_error'))
851 function form_error($field = '', $prefix = '', $suffix = '')
853 if (FALSE === ($OBJ =& _get_validation_object()))
858 return $OBJ->error($field, $prefix, $suffix);
862 // ------------------------------------------------------------------------
865 * Validation Error String
867 * Returns all the errors associated with a form submission. This is a helper
868 * function for the form validation class.
875 if ( ! function_exists('validation_errors'))
877 function validation_errors($prefix = '', $suffix = '')
879 if (FALSE === ($OBJ =& _get_validation_object()))
884 return $OBJ->error_string($prefix, $suffix);
888 // ------------------------------------------------------------------------
891 * Parse the form attributes
893 * Helper function used by some of the form helpers
900 if ( ! function_exists('_parse_form_attributes'))
902 function _parse_form_attributes($attributes, $default)
904 if (is_array($attributes))
906 foreach ($default as $key => $val)
908 if (isset($attributes[$key]))
910 $default[$key] = $attributes[$key];
911 unset($attributes[$key]);
915 if (count($attributes) > 0)
917 $default = array_merge($default, $attributes);
923 foreach ($default as $key => $val)
927 $val = form_prep($val, $default['name']);
930 $att .= $key . '="' . $val . '" ';
937 // ------------------------------------------------------------------------
940 * Attributes To String
942 * Helper function used by some of the form helpers
949 if ( ! function_exists('_attributes_to_string'))
951 function _attributes_to_string($attributes, $formtag = FALSE)
953 if (is_string($attributes) AND strlen($attributes) > 0)
955 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
957 $attributes .= ' method="post"';
960 return ' '.$attributes;
963 if (is_object($attributes) AND count($attributes) > 0)
965 $attributes = (array)$attributes;
968 if (is_array($attributes) AND count($attributes) > 0)
972 if ( ! isset($attributes['method']) AND $formtag === TRUE)
974 $atts .= ' method="post"';
977 foreach ($attributes as $key => $val)
979 $atts .= ' '.$key.'="'.$val.'"';
987 // ------------------------------------------------------------------------
992 * Determines what the form validation class was instantiated as, fetches
993 * the object and returns it.
998 if ( ! function_exists('_get_validation_object'))
1000 function &_get_validation_object()
1002 $CI =& get_instance();
1004 // We set this as a variable since we're returning by reference
1007 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
1012 $object = $CI->load->_ci_classes['form_validation'];
1014 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1019 return $CI->$object;
1024 /* End of file form_helper.php */
1025 /* Location: ./system/helpers/form_helper.php */