LSform: fix handling default values of dataEntryForm

This commit is contained in:
Benjamin Renard 2022-08-01 11:27:28 +02:00
parent e23ee2f209
commit a56b20728a

View file

@ -498,11 +498,13 @@ class LSform extends LSlog_staticLoggerClass {
}
}
else {
$elementsList = $this -> dataEntryFormConfig['displayedElements'];
if (isset($this -> dataEntryFormConfig['defaultValues']) && is_array($this -> dataEntryFormConfig['defaultValues'])) {
$this -> setPostData($this -> dataEntryFormConfig['defaultValues']);
$elementsList = array_merge($elementsList,array_keys($this -> dataEntryFormConfig['defaultValues']));
}
$defaultValues = LSconfig::get(
'defaultValues', array(), 'array', $this -> dataEntryFormConfig
);
$elementsList = array_merge(
$this -> dataEntryFormConfig['displayedElements'],
array_keys($this -> dataEntryFormConfig['defaultValues'])
);
foreach($elementsList as $elementName) {
if (!isset($this -> elements[$elementName])) {
@ -510,9 +512,21 @@ class LSform extends LSlog_staticLoggerClass {
continue;
}
$element = $this -> elements[$elementName];
if ((isset($this -> dataEntryFormConfig['requiredAllAttributes']) && $this -> dataEntryFormConfig['requiredAllAttributes']) || isset($this -> dataEntryFormConfig['requiredAttributes']) && is_array($this -> dataEntryFormConfig['requiredAttributes']) && in_array($elementName,$this -> dataEntryFormConfig['requiredAttributes'])) {
$element -> setRequired();
// Set required from dataEntryForm config
if (
LSconfig :: get('requiredAllAttributes', false, 'bool', $this -> dataEntryFormConfig) ||
in_array(
$elementName,
LSconfig :: get('requiredAttributes', array(), 'array', $this -> dataEntryFormConfig)
)
){
$element -> setRequired();
}
// Set default value if not present in POST data
if (!isset($_POST[$elementName]) && isset($defaultValues[$elementName])) {
$_POST[$elementName] = ensureIsArray($defaultValues[$elementName]);
}
// Retrieve POST data of the element
if( !($element -> getPostData($this -> _postData, $onlyIfPresent)) ) {
LSerror :: addErrorCode('LSform_02',$element_name);
return;