diff options
author | vlakoff <vlakoff@gmail.com> | 2013-07-27 10:07:43 +0200 |
---|---|---|
committer | vlakoff <vlakoff@gmail.com> | 2013-07-27 10:07:43 +0200 |
commit | ea19bc4f8fea2a7b6d0b1d85c279369ec8fce06e (patch) | |
tree | 6a43cbbfd5458207fa79d19bfc510312ff6c234b /system/helpers/form_helper.php | |
parent | 122ca9bd8b055eaabee2ec54f476749107533565 (diff) |
Form helper: refactor form_open() and _attributes_to_string()
Diffstat (limited to 'system/helpers/form_helper.php')
-rw-r--r-- | system/helpers/form_helper.php | 56 |
1 files changed, 18 insertions, 38 deletions
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 7f4276bc7..f28296c2e 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -50,21 +50,20 @@ if ( ! function_exists('form_open')) * @param array a key/value pair hidden data * @return string */ - function form_open($action = '', $attributes = '', $hidden = array()) + function form_open($action = '', $attributes = array(), $hidden = array()) { $CI =& get_instance(); - if (empty($attributes)) - { - $attributes = 'method="post"'; - } - elseif (is_array($attributes) && ! isset($attributes['method'])) + $attributes = _attributes_to_string($attributes); + + if (stripos($attributes, 'method=') === FALSE) { - $attributes['method'] = 'post'; + $attributes .= ' method="post"'; } - elseif (stripos($attributes, 'method=') === FALSE) + + if (stripos($attributes, 'accept-charset=') === FALSE) { - $attributes .= ' method="post"'; + $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; } // If an action is not a full URL then turn it into one @@ -78,7 +77,7 @@ if ( ! function_exists('form_open')) $action = $CI->config->site_url($CI->uri->uri_string()); } - $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n"; + $form = '<form action="'.$action.'"'.$attributes.">\n"; // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR stripos($form, 'method="get"'))) @@ -550,12 +549,12 @@ if ( ! function_exists('form_fieldset')) * use form_fieldset_close() * * @param string The legend text - * @param string Additional attributes + * @param array Additional attributes * @return string */ function form_fieldset($legend_text = '', $attributes = array()) { - $fieldset = '<fieldset'._attributes_to_string($attributes, FALSE).">\n"; + $fieldset = '<fieldset'._attributes_to_string($attributes).">\n"; if ($legend_text !== '') { return $fieldset.'<legend>'.$legend_text."</legend>\n"; @@ -928,45 +927,24 @@ if ( ! function_exists('_attributes_to_string')) * Helper function used by some of the form helpers * * @param mixed - * @param bool * @return string */ - function _attributes_to_string($attributes, $formtag = FALSE) + function _attributes_to_string($attributes) { - if (is_string($attributes) && strlen($attributes) > 0) + if (is_string($attributes)) { - if ($formtag === TRUE && strpos($attributes, 'method=') === FALSE) - { - $attributes .= ' method="post"'; - } - - if ($formtag === TRUE && strpos($attributes, 'accept-charset=') === FALSE) - { - $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; - } - - return ' '.$attributes; + return ($attributes === '' ? '' : ' '.$attributes); } - if (is_object($attributes) && count($attributes) > 0) + if (is_object($attributes)) { $attributes = (array) $attributes; } - if (is_array($attributes) && ($formtag === TRUE OR count($attributes) > 0)) + if (is_array($attributes)) { $atts = ''; - if ( ! isset($attributes['method']) && $formtag === TRUE) - { - $atts .= ' method="post"'; - } - - if ( ! isset($attributes['accept-charset']) && $formtag === TRUE) - { - $atts .= ' accept-charset="'.strtolower(config_item('charset')).'"'; - } - foreach ($attributes as $key => $val) { $atts .= ' '.$key.'="'.$val.'"'; @@ -974,6 +952,8 @@ if ( ! function_exists('_attributes_to_string')) return $atts; } + + return FALSE; } } |