1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
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
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 // If an action is not a full URL then turn it into one
53 if ($action && strpos($action, '://') === FALSE)
55 $action = $CI->config->site_url($action);
58 // If no action is provided then set to the current url
59 $action OR $action = $CI->config->site_url($CI->uri->uri_string());
61 $form = '<form action="'.$action.'"';
63 $form .= _attributes_to_string($attributes, TRUE);
68 if ($CI->config->item('csrf_protection') === TRUE)
70 $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash();
73 if (is_array($hidden) AND count($hidden) > 0)
75 $form .= sprintf("\n<div class=\"hidden\">%s</div>", form_hidden($hidden));
82 // ------------------------------------------------------------------------
85 * Form Declaration - Multipart type
87 * Creates the opening portion of the form, but with "multipart/form-data".
90 * @param string the URI segments of the form destination
91 * @param array a key/value pair of attributes
92 * @param array a key/value pair hidden data
95 if ( ! function_exists('form_open_multipart'))
97 function form_open_multipart($action, $attributes = array(), $hidden = array())
99 if (is_string($attributes))
101 $attributes .= ' enctype="multipart/form-data"';
105 $attributes['enctype'] = 'multipart/form-data';
108 return form_open($action, $attributes, $hidden);
112 // ------------------------------------------------------------------------
117 * Generates hidden fields. You can pass a simple key/value string or an associative
118 * array with multiple values.
125 if ( ! function_exists('form_hidden'))
127 function form_hidden($name, $value = '', $recursing = FALSE)
131 if ($recursing === FALSE)
138 foreach ($name as $key => $val)
140 form_hidden($key, $val, TRUE);
145 if ( ! is_array($value))
147 $form .= '<input type="hidden" name="'.$name.'" value="'.form_prep($value, $name).'" />'."\n";
151 foreach ($value as $k => $v)
153 $k = (is_int($k)) ? '' : $k;
154 form_hidden($name.'['.$k.']', $v, TRUE);
162 // ------------------------------------------------------------------------
173 if ( ! function_exists('form_input'))
175 function form_input($data = '', $value = '', $extra = '')
177 $defaults = array('type' => 'text', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
179 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
183 // ------------------------------------------------------------------------
188 * Identical to the input function but adds the "password" type
196 if ( ! function_exists('form_password'))
198 function form_password($data = '', $value = '', $extra = '')
200 if ( ! is_array($data))
202 $data = array('name' => $data);
205 $data['type'] = 'password';
206 return form_input($data, $value, $extra);
210 // ------------------------------------------------------------------------
215 * Identical to the input function but adds the "file" type
223 if ( ! function_exists('form_upload'))
225 function form_upload($data = '', $value = '', $extra = '')
227 if ( ! is_array($data))
229 $data = array('name' => $data);
232 $data['type'] = 'file';
233 return form_input($data, $value, $extra);
237 // ------------------------------------------------------------------------
248 if ( ! function_exists('form_textarea'))
250 function form_textarea($data = '', $value = '', $extra = '')
252 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'cols' => '90', 'rows' => '12');
254 if ( ! is_array($data) OR ! isset($data['value']))
260 $val = $data['value'];
261 unset($data['value']); // textareas don't use the value attribute
264 $name = (is_array($data)) ? $data['name'] : $data;
265 return "<textarea "._parse_form_attributes($data, $defaults).$extra.">".form_prep($val, $name)."</textarea>";
269 // ------------------------------------------------------------------------
281 if ( ! function_exists('form_multiselect'))
283 function form_multiselect($name = '', $options = array(), $selected = array(), $extra = '')
285 if ( ! strpos($extra, 'multiple'))
287 $extra .= ' multiple="multiple"';
290 return form_dropdown($name, $options, $selected, $extra);
294 // --------------------------------------------------------------------
306 if ( ! function_exists('form_dropdown'))
308 function form_dropdown($name = '', $options = array(), $selected = array(), $extra = '')
310 if ( ! is_array($selected))
312 $selected = array($selected);
315 // If no selected state was submitted we will attempt to set it automatically
316 if (count($selected) === 0)
318 // If the form name appears in the $_POST array we have a winner!
319 if (isset($_POST[$name]))
321 $selected = array($_POST[$name]);
325 if ($extra != '') $extra = ' '.$extra;
327 $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : '';
329 $form = '<select name="'.$name.'"'.$extra.$multiple.">\n";
331 foreach ($options as $key => $val)
333 $key = (string) $key;
335 if (is_array($val) && ! empty($val))
337 $form .= '<optgroup label="'.$key.'">'."\n";
339 foreach ($val as $optgroup_key => $optgroup_val)
341 $sel = (in_array($optgroup_key, $selected)) ? ' selected="selected"' : '';
343 $form .= '<option value="'.$optgroup_key.'"'.$sel.'>'.(string) $optgroup_val."</option>\n";
346 $form .= '</optgroup>'."\n";
350 $sel = (in_array($key, $selected)) ? ' selected="selected"' : '';
352 $form .= '<option value="'.$key.'"'.$sel.'>'.(string) $val."</option>\n";
356 $form .= '</select>';
362 // ------------------------------------------------------------------------
374 if ( ! function_exists('form_checkbox'))
376 function form_checkbox($data = '', $value = '', $checked = FALSE, $extra = '')
378 $defaults = array('type' => 'checkbox', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
380 if (is_array($data) AND array_key_exists('checked', $data))
382 $checked = $data['checked'];
384 if ($checked == FALSE)
386 unset($data['checked']);
390 $data['checked'] = 'checked';
394 if ($checked == TRUE)
396 $defaults['checked'] = 'checked';
400 unset($defaults['checked']);
403 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
407 // ------------------------------------------------------------------------
419 if ( ! function_exists('form_radio'))
421 function form_radio($data = '', $value = '', $checked = FALSE, $extra = '')
423 if ( ! is_array($data))
425 $data = array('name' => $data);
428 $data['type'] = 'radio';
429 return form_checkbox($data, $value, $checked, $extra);
433 // ------------------------------------------------------------------------
444 if ( ! function_exists('form_submit'))
446 function form_submit($data = '', $value = '', $extra = '')
448 $defaults = array('type' => 'submit', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
450 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
454 // ------------------------------------------------------------------------
465 if ( ! function_exists('form_reset'))
467 function form_reset($data = '', $value = '', $extra = '')
469 $defaults = array('type' => 'reset', 'name' => (( ! is_array($data)) ? $data : ''), 'value' => $value);
471 return "<input "._parse_form_attributes($data, $defaults).$extra." />";
475 // ------------------------------------------------------------------------
486 if ( ! function_exists('form_button'))
488 function form_button($data = '', $content = '', $extra = '')
490 $defaults = array('name' => (( ! is_array($data)) ? $data : ''), 'type' => 'button');
492 if ( is_array($data) AND isset($data['content']))
494 $content = $data['content'];
495 unset($data['content']); // content is not an attribute
498 return "<button "._parse_form_attributes($data, $defaults).$extra.">".$content."</button>";
502 // ------------------------------------------------------------------------
508 * @param string The text to appear onscreen
509 * @param string The id the label applies to
510 * @param string Additional attributes
513 if ( ! function_exists('form_label'))
515 function form_label($label_text = '', $id = '', $attributes = array())
522 $label .= " for=\"$id\"";
525 if (is_array($attributes) AND count($attributes) > 0)
527 foreach ($attributes as $key => $val)
529 $label .= ' '.$key.'="'.$val.'"';
533 $label .= ">$label_text</label>";
539 // ------------------------------------------------------------------------
543 * Used to produce <fieldset><legend>text</legend>. To close fieldset
544 * use form_fieldset_close()
547 * @param string The legend text
548 * @param string Additional attributes
551 if ( ! function_exists('form_fieldset'))
553 function form_fieldset($legend_text = '', $attributes = array())
555 $fieldset = "<fieldset";
557 $fieldset .= _attributes_to_string($attributes, FALSE);
561 if ($legend_text != '')
563 $fieldset .= "<legend>$legend_text</legend>\n";
570 // ------------------------------------------------------------------------
579 if ( ! function_exists('form_fieldset_close'))
581 function form_fieldset_close($extra = '')
583 return "</fieldset>".$extra;
587 // ------------------------------------------------------------------------
596 if ( ! function_exists('form_close'))
598 function form_close($extra = '')
600 return "</form>".$extra;
604 // ------------------------------------------------------------------------
609 * Formats text so that it can be safely placed in a form field in the event it has HTML tags.
615 if ( ! function_exists('form_prep'))
617 function form_prep($str = '', $field_name = '')
619 static $prepped_fields = array();
621 // if the field name is an array we do this recursively
624 foreach ($str as $key => $val)
626 $str[$key] = form_prep($val);
637 // we've already prepped a field with this name
638 // @todo need to figure out a way to namespace this so
639 // that we know the *exact* field and not just one with
641 if (isset($prepped_fields[$field_name]))
646 $str = htmlspecialchars($str);
648 // In case htmlspecialchars misses these.
649 $str = str_replace(array("'", '"'), array("'", """), $str);
651 if ($field_name != '')
653 $prepped_fields[$field_name] = $field_name;
660 // ------------------------------------------------------------------------
665 * Grabs a value from the POST array for the specified field so you can
666 * re-populate an input field or textarea. If Form Validation
667 * is active it retrieves the info from the validation class
673 if ( ! function_exists('set_value'))
675 function set_value($field = '', $default = '')
677 if (FALSE === ($OBJ =& _get_validation_object()))
679 if ( ! isset($_POST[$field]))
684 return form_prep($_POST[$field], $field);
687 return form_prep($OBJ->set_value($field, $default), $field);
691 // ------------------------------------------------------------------------
696 * Let's you set the selected value of a <select> menu via data in the POST array.
697 * If Form Validation is active it retrieves the info from the validation class
705 if ( ! function_exists('set_select'))
707 function set_select($field = '', $value = '', $default = FALSE)
709 $OBJ =& _get_validation_object();
713 if ( ! isset($_POST[$field]))
715 if (count($_POST) === 0 AND $default == TRUE)
717 return ' selected="selected"';
722 $field = $_POST[$field];
724 if (is_array($field))
726 if ( ! in_array($value, $field))
733 if (($field == '' OR $value == '') OR ($field != $value))
739 return ' selected="selected"';
742 return $OBJ->set_select($field, $value, $default);
746 // ------------------------------------------------------------------------
751 * Let's you set the selected value of a checkbox via the value in the POST array.
752 * If Form Validation is active it retrieves the info from the validation class
760 if ( ! function_exists('set_checkbox'))
762 function set_checkbox($field = '', $value = '', $default = FALSE)
764 $OBJ =& _get_validation_object();
768 if ( ! isset($_POST[$field]))
770 if (count($_POST) === 0 AND $default == TRUE)
772 return ' checked="checked"';
777 $field = $_POST[$field];
779 if (is_array($field))
781 if ( ! in_array($value, $field))
788 if (($field == '' OR $value == '') OR ($field != $value))
794 return ' checked="checked"';
797 return $OBJ->set_checkbox($field, $value, $default);
801 // ------------------------------------------------------------------------
806 * Let's you set the selected value of a radio field via info in the POST array.
807 * If Form Validation is active it retrieves the info from the validation class
815 if ( ! function_exists('set_radio'))
817 function set_radio($field = '', $value = '', $default = FALSE)
819 $OBJ =& _get_validation_object();
823 if ( ! isset($_POST[$field]))
825 if (count($_POST) === 0 AND $default == TRUE)
827 return ' checked="checked"';
832 $field = $_POST[$field];
834 if (is_array($field))
836 if ( ! in_array($value, $field))
843 if (($field == '' OR $value == '') OR ($field != $value))
849 return ' checked="checked"';
852 return $OBJ->set_radio($field, $value, $default);
856 // ------------------------------------------------------------------------
861 * Returns the error for a specific form field. This is a helper for the
862 * form validation class.
870 if ( ! function_exists('form_error'))
872 function form_error($field = '', $prefix = '', $suffix = '')
874 if (FALSE === ($OBJ =& _get_validation_object()))
879 return $OBJ->error($field, $prefix, $suffix);
883 // ------------------------------------------------------------------------
886 * Validation Error String
888 * Returns all the errors associated with a form submission. This is a helper
889 * function for the form validation class.
896 if ( ! function_exists('validation_errors'))
898 function validation_errors($prefix = '', $suffix = '')
900 if (FALSE === ($OBJ =& _get_validation_object()))
905 return $OBJ->error_string($prefix, $suffix);
909 // ------------------------------------------------------------------------
912 * Parse the form attributes
914 * Helper function used by some of the form helpers
921 if ( ! function_exists('_parse_form_attributes'))
923 function _parse_form_attributes($attributes, $default)
925 if (is_array($attributes))
927 foreach ($default as $key => $val)
929 if (isset($attributes[$key]))
931 $default[$key] = $attributes[$key];
932 unset($attributes[$key]);
936 if (count($attributes) > 0)
938 $default = array_merge($default, $attributes);
944 foreach ($default as $key => $val)
948 $val = form_prep($val, $default['name']);
951 $att .= $key . '="' . $val . '" ';
958 // ------------------------------------------------------------------------
961 * Attributes To String
963 * Helper function used by some of the form helpers
970 if ( ! function_exists('_attributes_to_string'))
972 function _attributes_to_string($attributes, $formtag = FALSE)
974 if (is_string($attributes) AND strlen($attributes) > 0)
976 if ($formtag == TRUE AND strpos($attributes, 'method=') === FALSE)
978 $attributes .= ' method="post"';
981 if ($formtag == TRUE AND strpos($attributes, 'accept-charset=') === FALSE)
983 $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"';
986 return ' '.$attributes;
989 if (is_object($attributes) AND count($attributes) > 0)
991 $attributes = (array)$attributes;
994 if (is_array($attributes) AND count($attributes) > 0)
998 if ( ! isset($attributes['method']) AND $formtag === TRUE)
1000 $atts .= ' method="post"';
1003 if ( ! isset($attributes['accept-charset']) AND $formtag === TRUE)
1005 $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"';
1008 foreach ($attributes as $key => $val)
1010 $atts .= ' '.$key.'="'.$val.'"';
1018 // ------------------------------------------------------------------------
1023 * Determines what the form validation class was instantiated as, fetches
1024 * the object and returns it.
1029 if ( ! function_exists('_get_validation_object'))
1031 function &_get_validation_object()
1033 $CI =& get_instance();
1035 // We set this as a variable since we're returning by reference
1038 if ( ! isset($CI->load->_ci_classes) OR ! isset($CI->load->_ci_classes['form_validation']))
1043 $object = $CI->load->_ci_classes['form_validation'];
1045 if ( ! isset($CI->$object) OR ! is_object($CI->$object))
1050 return $CI->$object;
1055 /* End of file form_helper.php */
1056 /* Location: ./system/helpers/form_helper.php */