From 519f87a07bd1fe3a9ec037f727628bb6c7c8e251 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 23 Jul 2013 17:16:10 +0300 Subject: Loader changes & optimizations related to issue #2551 --- system/helpers/form_helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 2002d4269..bc14df221 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -988,7 +988,7 @@ if ( ! function_exists('_get_validation_object')) // We set this as a variable since we're returning by reference. $return = FALSE; - if (FALSE !== ($object = $CI->load->is_loaded('form_validation'))) + if (FALSE !== ($object = $CI->load->is_loaded('Form_validation'))) { if ( ! isset($CI->$object) OR ! is_object($CI->$object)) { -- cgit v1.2.3-24-g4f1b From 122ca9bd8b055eaabee2ec54f476749107533565 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 26 Jul 2013 18:16:26 +0300 Subject: Fix #2560 --- system/helpers/form_helper.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index bc14df221..7f4276bc7 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -54,10 +54,18 @@ if ( ! function_exists('form_open')) { $CI =& get_instance(); - if ($attributes === '') + 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) @@ -73,7 +81,7 @@ if ( ! function_exists('form_open')) $form = '
\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 strpos($form, 'method="get"'))) + if ($CI->config->item('csrf_protection') === TRUE && ! (strpos($action, $CI->config->base_url()) === FALSE OR stripos($form, 'method="get"'))) { $hidden[$CI->security->get_csrf_token_name()] = $CI->security->get_csrf_hash(); } -- cgit v1.2.3-24-g4f1b From ea19bc4f8fea2a7b6d0b1d85c279369ec8fce06e Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 27 Jul 2013 10:07:43 +0200 Subject: Form helper: refactor form_open() and _attributes_to_string() --- system/helpers/form_helper.php | 56 ++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 38 deletions(-) (limited to 'system/helpers/form_helper.php') 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 = '\n"; + $form = '\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 = '\n"; + $fieldset = '\n"; if ($legend_text !== '') { return $fieldset.''.$legend_text."\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; } } -- cgit v1.2.3-24-g4f1b From c4f9c62a604079fe3c2ab7637ffad894188fb429 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 27 Jul 2013 10:08:00 +0200 Subject: More logical order --- system/helpers/form_helper.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index f28296c2e..6fca73f85 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -54,6 +54,17 @@ if ( ! function_exists('form_open')) { $CI =& get_instance(); + // If an action is not a full URL then turn it into one + if ($action && strpos($action, '://') === FALSE) + { + $action = $CI->config->site_url($action); + } + elseif ( ! $action) + { + // If no action is provided then set to the current url + $action = $CI->config->site_url($CI->uri->uri_string()); + } + $attributes = _attributes_to_string($attributes); if (stripos($attributes, 'method=') === FALSE) @@ -66,17 +77,6 @@ if ( ! function_exists('form_open')) $attributes .= ' accept-charset="'.strtolower(config_item('charset')).'"'; } - // If an action is not a full URL then turn it into one - if ($action && strpos($action, '://') === FALSE) - { - $action = $CI->config->site_url($action); - } - elseif ( ! $action) - { - // If no action is provided then set to the current url - $action = $CI->config->site_url($CI->uri->uri_string()); - } - $form = '\n"; // Add CSRF field if enabled, but leave it out for GET requests and requests to external websites -- cgit v1.2.3-24-g4f1b From f746475e80a2734277eb1e76bb916ae3b2863423 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sun, 28 Jul 2013 22:23:21 +0200 Subject: Form helper _attributes_to_string() micro-optimization As $attributes should be most of the times an array, let's save an is_string() call. --- system/helpers/form_helper.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 6fca73f85..5ba5b556c 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -931,11 +931,6 @@ if ( ! function_exists('_attributes_to_string')) */ function _attributes_to_string($attributes) { - if (is_string($attributes)) - { - return ($attributes === '' ? '' : ' '.$attributes); - } - if (is_object($attributes)) { $attributes = (array) $attributes; @@ -953,6 +948,11 @@ if ( ! function_exists('_attributes_to_string')) return $atts; } + if (is_string($attributes)) + { + return ($attributes === '' ? '' : ' '.$attributes); + } + return FALSE; } } -- cgit v1.2.3-24-g4f1b From bb8b08982369dd6a2d321844dced488f92134f20 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sun, 28 Jul 2013 22:35:04 +0200 Subject: Polishing Form helper --- system/helpers/form_helper.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 5ba5b556c..146c0f588 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -931,6 +931,11 @@ if ( ! function_exists('_attributes_to_string')) */ function _attributes_to_string($attributes) { + if (empty($attributes)) + { + return ''; + } + if (is_object($attributes)) { $attributes = (array) $attributes; @@ -950,7 +955,7 @@ if ( ! function_exists('_attributes_to_string')) if (is_string($attributes)) { - return ($attributes === '' ? '' : ' '.$attributes); + return ' '.$attributes; } return FALSE; -- cgit v1.2.3-24-g4f1b From ae50f5537718431af05037c857d1c303e25a76f6 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Sep 2013 16:17:41 +0300 Subject: Fix #2639 --- system/helpers/form_helper.php | 79 ++++++++++-------------------------------- 1 file changed, 19 insertions(+), 60 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 146c0f588..424bb7e64 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -726,37 +726,18 @@ if ( ! function_exists('set_checkbox')) */ function set_checkbox($field = '', $value = '', $default = FALSE) { - $OBJ =& _get_validation_object(); + $CI =& get_instance(); - if ($OBJ === FALSE) + if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field)) { - if ( ! isset($_POST[$field])) - { - if (count($_POST) === 0 && $default === TRUE) - { - return ' checked="checked"'; - } - return ''; - } - - $field = $_POST[$field]; - - if (is_array($field)) - { - if ( ! in_array($value, $field)) - { - return ''; - } - } - elseif (($field == '' OR $value == '') OR $field !== $value) - { - return ''; - } - - return ' checked="checked"'; + return $CI->form_validation->set_checkbox($field, $value, $default); + } + elseif (($input = $CI->input->post($field, FALSE)) === NULL) + { + return ($default === TRUE) ? ' checked="checked"' : ''; } - return $OBJ->set_checkbox($field, $value, $default); + return ($input === $value) ? ' checked="checked"' : ''; } } @@ -770,47 +751,25 @@ if ( ! function_exists('set_radio')) * Let's you set the selected value of a radio field via info in the POST array. * If Form Validation is active it retrieves the info from the validation class * - * @param string - * @param string - * @param bool + * @param string $field + * @param string $value + * @param bool $default * @return string */ function set_radio($field = '', $value = '', $default = FALSE) { - $OBJ =& _get_validation_object(); + $CI =& get_instance(); - if ($OBJ === FALSE) + if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field)) { - if ( ! isset($_POST[$field])) - { - if (count($_POST) === 0 && $default === TRUE) - { - return ' checked="checked"'; - } - return ''; - } - - $field = $_POST[$field]; - - if (is_array($field)) - { - if ( ! in_array($value, $field)) - { - return ''; - } - } - else - { - if (($field == '' OR $value == '') OR $field !== $value) - { - return ''; - } - } - - return ' checked="checked"'; + return $CI->form_validation->set_radio($field, $value, $default); + } + elseif (($input = $CI->input->post($field, FALSE)) === NULL) + { + return ($default === TRUE) ? ' checked="checked"' : ''; } - return $OBJ->set_radio($field, $value, $default); + return ($input === $value) ? ' checked="checked"' : ''; } } -- cgit v1.2.3-24-g4f1b From 67f6a5e0321cc5d71dc2adc8dc72c71e96408dac Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Sep 2013 16:21:31 +0300 Subject: Fix array notation fields for set_select() as well --- system/helpers/form_helper.php | 35 ++++++++--------------------------- 1 file changed, 8 insertions(+), 27 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 424bb7e64..20379efa7 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -675,37 +675,18 @@ if ( ! function_exists('set_select')) */ function set_select($field = '', $value = '', $default = FALSE) { - $OBJ =& _get_validation_object(); + $CI =& get_instance(); - if ($OBJ === FALSE) + if (isset($CI->form_validation) && is_object($CI->form_validation) && $CI->form_validation->has_rule($field)) { - if ( ! isset($_POST[$field])) - { - if (count($_POST) === 0 && $default === TRUE) - { - return ' selected="selected"'; - } - return ''; - } - - $field = $_POST[$field]; - - if (is_array($field)) - { - if ( ! in_array($value, $field)) - { - return ''; - } - } - elseif (($field == '' OR $value == '') OR $field !== $value) - { - return ''; - } - - return ' selected="selected"'; + return $CI->form_validation->set_select($field, $value, $default); + } + elseif (($input = $CI->input->post($field, FALSE)) === NULL) + { + return ($default === TRUE) ? ' selected="selected"' : ''; } - return $OBJ->set_select($field, $value, $default); + return ($input === $value) ? ' checked="selected"' : ''; } } -- cgit v1.2.3-24-g4f1b From e8a23a532a4974773067903bf96200fa12fa4d41 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Sep 2013 18:29:29 +0300 Subject: An update to the #2639 fix --- system/helpers/form_helper.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 20379efa7..7112a99b7 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -717,6 +717,10 @@ if ( ! function_exists('set_checkbox')) { return ($default === TRUE) ? ' checked="checked"' : ''; } + elseif (is_array($input) && in_array($value, $input, TRUE)) + { + return ' checked="checked"'; + } return ($input === $value) ? ' checked="checked"' : ''; } -- cgit v1.2.3-24-g4f1b From d3b7e24b708623be425dc03c3bb429bf0e4741c8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 13 Sep 2013 18:36:29 +0300 Subject: Another one following #2639 --- system/helpers/form_helper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 7112a99b7..0cc5bd157 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -685,8 +685,12 @@ if ( ! function_exists('set_select')) { return ($default === TRUE) ? ' selected="selected"' : ''; } + elseif (is_array($input) && in_array($value, $input, TRUE)) + { + return ' selected="selected"'; + } - return ($input === $value) ? ' checked="selected"' : ''; + return ($input === $value) ? ' selected="selected"' : ''; } } -- cgit v1.2.3-24-g4f1b From 8e7cc7a18086ad32c8e13525b643aadde054bf40 Mon Sep 17 00:00:00 2001 From: Ahmad Anbar Date: Fri, 4 Oct 2013 02:45:28 +0300 Subject: parse $extra attributes in form_dropdown. --- system/helpers/form_helper.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 0cc5bd157..85f1f4e01 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -328,11 +328,8 @@ if ( ! function_exists('form_dropdown')) { $selected = array($_POST[$name]); } - - if ($extra != '') - { - $extra = ' '.$extra; - } + + $extra = _attributes_to_string($extra); $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; -- cgit v1.2.3-24-g4f1b From 7f5f8aaa01764f266b41791568863ec6bfda7e83 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Mon, 21 Oct 2013 14:37:40 +0300 Subject: Manually apply PR #2656 Fixes an 'Array to string conversion' notice in form_dropdown() --- system/helpers/form_helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 85f1f4e01..400a91faa 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -316,7 +316,7 @@ if ( ! function_exists('form_dropdown')) { isset($name['options']) OR $name['options'] = array(); isset($name['selected']) OR $name['selected'] = array(); - isset($name['extra']) OR $name['extra'] = array(); + isset($name['extra']) OR $name['extra'] = ''; return form_dropdown($name['name'], $name['options'], $name['selected'], $name['extra']); } @@ -328,7 +328,7 @@ if ( ! function_exists('form_dropdown')) { $selected = array($_POST[$name]); } - + $extra = _attributes_to_string($extra); $multiple = (count($selected) > 1 && strpos($extra, 'multiple') === FALSE) ? ' multiple="multiple"' : ''; -- cgit v1.2.3-24-g4f1b From a587a939ce0b8e7d1dfe0830ac83d881e151d6e0 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Wed, 23 Oct 2013 19:57:46 +0300 Subject: Fix issue #2695 --- system/helpers/form_helper.php | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'system/helpers/form_helper.php') diff --git a/system/helpers/form_helper.php b/system/helpers/form_helper.php index 400a91faa..a3d299b0d 100644 --- a/system/helpers/form_helper.php +++ b/system/helpers/form_helper.php @@ -682,9 +682,20 @@ if ( ! function_exists('set_select')) { return ($default === TRUE) ? ' selected="selected"' : ''; } - elseif (is_array($input) && in_array($value, $input, TRUE)) + + $value = (string) $value; + if (is_array($input)) { - return ' selected="selected"'; + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($input as &$v) + { + if ($value === $v) + { + return ' selected="selected"'; + } + } + + return ''; } return ($input === $value) ? ' selected="selected"' : ''; @@ -718,9 +729,20 @@ if ( ! function_exists('set_checkbox')) { return ($default === TRUE) ? ' checked="checked"' : ''; } - elseif (is_array($input) && in_array($value, $input, TRUE)) + + $value = (string) $value; + if (is_array($input)) { - return ' checked="checked"'; + // Note: in_array('', array(0)) returns TRUE, do not use it + foreach ($input as &$v) + { + if ($value === $v) + { + return ' checked="checked"'; + } + } + + return ''; } return ($input === $value) ? ' checked="checked"' : ''; @@ -755,7 +777,7 @@ if ( ! function_exists('set_radio')) return ($default === TRUE) ? ' checked="checked"' : ''; } - return ($input === $value) ? ' checked="checked"' : ''; + return ($input === (string) $value) ? ' checked="checked"' : ''; } } -- cgit v1.2.3-24-g4f1b