Skip to content

Commit 5141346

Browse files
[BUGFIX] Do not render empty eval and is_in instructions
This commit checks whether `eval` and `is_in` are not empty before they are used as "input params" for the JavaScript part of the FormEngine. Also, a plausibility check is added to ensure `eval=is_in` and a non-falsy `is_in` configuration are given. Resolves: #103184 Releases: main, 12.4 Change-Id: I23da4483f1b35b0720ce59d07847fe01ead9a461 Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83085 Reviewed-by: Andreas Kienast <[email protected]> Tested-by: core-ci <[email protected]> Reviewed-by: Markus Klein <[email protected]> Tested-by: Oliver Bartsch <[email protected]> Tested-by: Andreas Kienast <[email protected]> Reviewed-by: Oliver Bartsch <[email protected]> Tested-by: Markus Klein <[email protected]>
1 parent ca7baa3 commit 5141346

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

Build/Sources/TypeScript/backend/form-engine-validation.ts

+25-18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { selector } from '@typo3/core/literals';
2929

3030
type FormEngineFieldElement = HTMLInputElement|HTMLTextAreaElement|HTMLSelectElement;
3131
type CustomEvaluationCallback = (value: string) => string;
32+
type FormEngineInputParams = { field: string, evalList?: string, is_in?: string };
3233

3334
export default (function() {
3435

@@ -106,12 +107,7 @@ export default (function() {
106107

107108
if (field.dataset.config !== undefined) {
108109
const config = JSON.parse(field.dataset.config);
109-
const evalList = Utility.trimExplode(',', config.evalList);
110-
let value = field.value;
111-
112-
for (let i = 0; i < evalList.length; i++) {
113-
value = FormEngineValidation.formatValue(evalList[i], value, config);
114-
}
110+
const value = FormEngineValidation.formatByEvals(config, field.value);
115111
if (value.length) {
116112
humanReadableField.value = value;
117113
}
@@ -131,6 +127,16 @@ export default (function() {
131127
}
132128
};
133129

130+
FormEngineValidation.formatByEvals = function(config: FormEngineInputParams, value: string): string {
131+
if (config.evalList !== undefined) {
132+
const evalList = Utility.trimExplode(',', config.evalList);
133+
for (const evalInstruction of evalList) {
134+
value = FormEngineValidation.formatValue(evalInstruction, value, config);
135+
}
136+
}
137+
return value;
138+
};
139+
134140
/**
135141
* Format field value
136142
*/
@@ -206,17 +212,8 @@ export default (function() {
206212

207213
if (field.dataset.config !== undefined) {
208214
const config = JSON.parse(field.dataset.config);
209-
const evalList = Utility.trimExplode(',', config.evalList);
210-
let newValue = humanReadableField.value;
211-
212-
for (let i = 0; i < evalList.length; i++) {
213-
newValue = FormEngineValidation.processValue(evalList[i], newValue, config);
214-
}
215-
216-
let formattedValue = newValue;
217-
for (let i = 0; i < evalList.length; i++) {
218-
formattedValue = FormEngineValidation.formatValue(evalList[i], formattedValue, config);
219-
}
215+
const newValue = FormEngineValidation.processByEvals(config, humanReadableField.value);
216+
const formattedValue = FormEngineValidation.formatByEvals(config, newValue);
220217

221218
// Only update fields if value actually changed
222219
if (field.value !== newValue) {
@@ -402,10 +399,20 @@ export default (function() {
402399
return returnValue;
403400
};
404401

402+
FormEngineValidation.processByEvals = function(config: FormEngineInputParams, value: string): string {
403+
if (config.evalList !== undefined) {
404+
const evalList = Utility.trimExplode(',', config.evalList);
405+
for (const evalInstruction of evalList) {
406+
value = FormEngineValidation.processValue(evalInstruction, value, config);
407+
}
408+
}
409+
return value;
410+
};
411+
405412
/**
406413
* Process a value by given command and config
407414
*/
408-
FormEngineValidation.processValue = function(command: string, value: string, config: {is_in: string}): string {
415+
FormEngineValidation.processValue = function(command: string, value: string, config: FormEngineInputParams): string {
409416
let newString = '';
410417
let theValue = '';
411418
let a = 0;

typo3/sysext/backend/Classes/Form/Element/InputTextElement.php

+19-5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ public function render(): array
131131
$evalList[] = 'null';
132132
}
133133

134+
$formEngineInputParams = [
135+
'field' => $itemName,
136+
];
137+
// The `is_in` constraint requires two parameters to work: the "eval" setting and a configuration of the
138+
// actually allowed characters
139+
if (in_array('is_in', $evalList, true)) {
140+
if (($config['is_in'] ?? '') !== '') {
141+
$formEngineInputParams['is_in'] = $config['is_in'];
142+
} else {
143+
$evalList = array_diff($evalList, ['is_in']);
144+
}
145+
} else {
146+
unset($config['is_in']);
147+
}
148+
if ($evalList !== []) {
149+
$formEngineInputParams['evalList'] = implode(',', $evalList);
150+
}
151+
134152
$attributes = [
135153
'value' => '',
136154
'id' => $fieldId,
@@ -141,11 +159,7 @@ public function render(): array
141159
'hasDefaultValue',
142160
]),
143161
'data-formengine-validation-rules' => $this->getValidationDataAsJsonString($config),
144-
'data-formengine-input-params' => (string)json_encode([
145-
'field' => $itemName,
146-
'evalList' => implode(',', $evalList),
147-
'is_in' => trim($config['is_in'] ?? ''),
148-
], JSON_THROW_ON_ERROR),
162+
'data-formengine-input-params' => (string)json_encode($formEngineInputParams, JSON_THROW_ON_ERROR),
149163
'data-formengine-input-name' => $itemName,
150164
];
151165

0 commit comments

Comments
 (0)