diff options
author | Andrey Andreev <narf@devilix.net> | 2013-07-27 11:48:44 +0200 |
---|---|---|
committer | Andrey Andreev <narf@devilix.net> | 2013-07-27 11:48:44 +0200 |
commit | 627ef043db42b0cfee2620875339f488268519f1 (patch) | |
tree | 2677c0e26b51f8b78e9326b316cde51ac2dd8c07 /system/helpers/form_helper.php | |
parent | 122ca9bd8b055eaabee2ec54f476749107533565 (diff) | |
parent | c4f9c62a604079fe3c2ab7637ffad894188fb429 (diff) |
Merge pull request #2565 from vlakoff/develop
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 | 66 |
1 files changed, 23 insertions, 43 deletions
diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 7f4276bc7..6fca73f85 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -50,23 +50,10 @@ 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['method'] = 'post'; - } - elseif (stripos($attributes, 'method=') === FALSE) - { - $attributes .= ' method="post"'; - } - // If an action is not a full URL then turn it into one if ($action && strpos($action, '://') === FALSE) { @@ -78,7 +65,19 @@ if ( ! function_exists('form_open')) $action = $CI->config->site_url($CI->uri->uri_string()); } - $form = '<form action="'.$action.'"'._attributes_to_string($attributes, TRUE).">\n"; + $attributes = _attributes_to_string($attributes); + + if (stripos($attributes, 'method=') === FALSE) + { + $attributes .= ' method="post"'; + } + + if (stripos($attributes, 'accept-charset=') === FALSE) + { + $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; + } + + $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; } } |