LSformElement::password: fix changeInput feature

Fix error in modify form when object's attribute is not already set.
This commit is contained in:
Benjamin Renard 2020-06-30 15:55:21 +02:00
parent 5872430863
commit 5696eb99c4
2 changed files with 46 additions and 18 deletions

View file

@ -220,26 +220,42 @@ class LSformElement extends LSlog_staticLoggerClass {
if($this -> isFreeze()) { if($this -> isFreeze()) {
return true; return true;
} }
if (isset($_POST[$this -> name])) { $return[$this -> name] = self :: getData($_POST, $this -> name);
$return[$this -> name]=array(); if (!is_array($return[$this -> name])) {
if(!is_array($_POST[$this -> name])) { if ($onlyIfPresent) {
$_POST[$this -> name] = array($_POST[$this -> name]); 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"))) { if (!empty($val)||(is_string($val)&&($val=="0"))) {
$return[$this -> name][$key] = $val; $return[$key] = $val;
} }
} }
return true; return $return;
}
elseif ($onlyIfPresent) {
self :: log_debug($this -> name.": not in POST data => ignore it");
return true;
}
else {
$return[$this -> name] = array();
return true;
} }
return false;
} }
/** /**

View file

@ -63,16 +63,28 @@ class LSformElement_password extends LSformElement {
} }
if ($this -> getParam('html_options.confirmInput', False, 'bool')) { if ($this -> getParam('html_options.confirmInput', False, 'bool')) {
$confirm_name = $this -> name . '_confirm'; $confirm_data = self :: getData($_POST, $this -> name . '_confirm');
if (!isset($_POST[$confirm_name]) || !$_POST[$confirm_name] || $_POST[$confirm_name] != $return[$this -> name]) { $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]); 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.')); $this -> form -> setElementError($this -> attr_html, _('%{label}: passwords entered did not match.'));
return true; return true;
} }
} }
if ($this -> verifyPassword($return[$this -> name][0]) || (empty($return[$this -> name][0]) && empty($val))) { 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]); unset($return[$this -> name]);
$this -> form -> _notUpdate[$this -> name] = true; $this -> form -> _notUpdate[$this -> name] = true;
return true; return true;