From 5696eb99c462dc6f2402a6d379c036a527ddc580 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 30 Jun 2020 15:55:21 +0200 Subject: [PATCH] LSformElement::password: fix changeInput feature Fix error in modify form when object's attribute is not already set. --- src/includes/class/class.LSformElement.php | 46 +++++++++++++------ .../class/class.LSformElement_password.php | 18 ++++++-- 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/includes/class/class.LSformElement.php b/src/includes/class/class.LSformElement.php index d8e98a56..87073fcf 100644 --- a/src/includes/class/class.LSformElement.php +++ b/src/includes/class/class.LSformElement.php @@ -220,26 +220,42 @@ class LSformElement extends LSlog_staticLoggerClass { if($this -> isFreeze()) { return true; } - if (isset($_POST[$this -> name])) { - $return[$this -> name]=array(); - if(!is_array($_POST[$this -> name])) { - $_POST[$this -> name] = array($_POST[$this -> name]); + $return[$this -> name] = self :: getData($_POST, $this -> name); + if (!is_array($return[$this -> name])) { + if ($onlyIfPresent) { + self :: log_debug($this -> name.": not in POST data => ignore it"); + unset($return[$this -> name]); } - foreach($_POST[$this -> name] as $key => $val) { + else { + $return[$this -> name] = array(); + } + } + return true; + } + + /** + * Retreive the value of the element specified by its name ($name) + * from POST data (provided as $post). + * + * @param[in] &$post array Reference of the array for input POST data + * @param[in] $name string POST data element name + * + * @retval mixed Array of POST data value if present, false otherwise + */ + protected static function getData(&$post, $name) { + if (isset($post[$name])) { + $return=array(); + if(!is_array($post[$name])) { + $post[$name] = array($post[$name]); + } + foreach($post[$name] as $key => $val) { if (!empty($val)||(is_string($val)&&($val=="0"))) { - $return[$this -> name][$key] = $val; + $return[$key] = $val; } } - return true; - } - elseif ($onlyIfPresent) { - self :: log_debug($this -> name.": not in POST data => ignore it"); - return true; - } - else { - $return[$this -> name] = array(); - return true; + return $return; } + return false; } /** diff --git a/src/includes/class/class.LSformElement_password.php b/src/includes/class/class.LSformElement_password.php index 69d00c56..0bfeec1b 100644 --- a/src/includes/class/class.LSformElement_password.php +++ b/src/includes/class/class.LSformElement_password.php @@ -63,16 +63,28 @@ class LSformElement_password extends LSformElement { } if ($this -> getParam('html_options.confirmInput', False, 'bool')) { - $confirm_name = $this -> name . '_confirm'; - if (!isset($_POST[$confirm_name]) || !$_POST[$confirm_name] || $_POST[$confirm_name] != $return[$this -> name]) { + $confirm_data = self :: getData($_POST, $this -> name . '_confirm'); + $confirmed = false; + if (!is_array($confirm_data)) { + if (!isset($return[$this -> name]) || empty($return[$this -> name]) || empty($return[$this -> name][0])) { + self :: log_debug('getPostData('.$this -> name.'): no confirm data, but empty password provided => confirmed'); + $confirmed = true; + } + } + elseif ($confirm_data == $return[$this -> name]) { + self :: log_debug('getPostData('.$this -> name.'): confirm password value matched with new password'); + $confirmed = true; + } + if (!$confirmed) { unset($return[$this -> name]); + self :: log_debug('getPostData('.$this -> name.'): '.varDump($return[$this -> name])." != ".varDump($confirm_data)); $this -> form -> setElementError($this -> attr_html, _('%{label}: passwords entered did not match.')); return true; } } if ($this -> verifyPassword($return[$this -> name][0]) || (empty($return[$this -> name][0]) && empty($val))) { - LSdebug("Password : no change"); + self :: log_debug('getPostData('.$this -> name.'): no change'); unset($return[$this -> name]); $this -> form -> _notUpdate[$this -> name] = true; return true;