LSformElement::jsonCompositeAttribute: fix handling empty value & $onlyIfPresent parameter

This commit is contained in:
Benjamin Renard 2022-10-17 17:29:36 +02:00
parent a0ceb1dbdc
commit fd17f87a57

View file

@ -242,18 +242,24 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
// Extract value form POST data // Extract value form POST data
$parseValues = array(); $parseValues = array();
$present = false;
// API mode // API mode
if ($this -> form -> api_mode) { if ($this -> form -> api_mode) {
$present = isset($_POST[$this -> name]);
$json_values = $this -> getData($_POST, $this -> name); $json_values = $this -> getData($_POST, $this -> name);
if (!is_array($json_values) || empty($json_values)) { self :: log_trace(
self :: log_trace($this." -> getPostData(): not in POST data"); $this." -> getPostData(onlyIfPresent=".($onlyIfPresent?1:0)."): ".
return true; "raw JSON values = ".varDump($json_values)
} );
$json_value_count = 0; $json_value_count = 0;
foreach($json_values as $json_value) { foreach((is_array($json_values)?$json_values:array()) as $json_value) {
$json_value_count += 1; $json_value_count += 1;
$input_value = json_decode($json_value, true); $input_value = json_decode($json_value, true);
self :: log_trace(
$this." -> getPostData(onlyIfPresent=".($onlyIfPresent?1:0)."): ".
"raw JSON value #$json_value_count = ".varDump($input_value)
);
if (!is_array($input_value)) { if (!is_array($input_value)) {
$this -> form -> setElementError( $this -> form -> setElementError(
$this -> attr_html, $this -> attr_html,
@ -290,13 +296,19 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
} }
// Ignore empty value from form // Ignore empty value from form
if (empty($unemptyComponents)) if (empty($unemptyComponents)) {
$this -> form -> setElementError(
$this -> attr_html,
getFData(_('JSON value #%{idx} is invalid (or empty).'), $json_value_count)
);
continue; continue;
}
$parseValues[] = $parseValue; $parseValues[] = $parseValue;
} }
} }
elseif (is_array($_POST[$this -> name.'__values_uuid'])) { elseif (is_array($_POST[$this -> name.'__values_uuid'])) {
$present = true;
// HTML form mode // HTML form mode
foreach ($_POST[$this -> name.'__values_uuid'] as $uuid) { foreach ($_POST[$this -> name.'__values_uuid'] as $uuid) {
$parseValue = array(); $parseValue = array();
@ -330,6 +342,7 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
} }
} }
$return[$this -> name] = array();
// Check extracted values // Check extracted values
foreach ($parseValues as $parseValue) { foreach ($parseValues as $parseValue) {
// Check component value // Check component value
@ -351,6 +364,14 @@ class LSformElement_jsonCompositeAttribute extends LSformElement {
$return[$this -> name][] = json_encode($parseValue); $return[$this -> name][] = json_encode($parseValue);
} }
if (!$present && $onlyIfPresent) {
self :: log_trace(
$this." -> getPostData(onlyIfPresent=".($onlyIfPresent?1:0)."): ".
"not present in POST data, ignore it."
);
unset($return[$this -> name]);
}
return true; return true;
} }