Skip to content

Commit f05c1b9

Browse files
author
David Connelly
committed
Correction to new post() function. Now returns empty string upon not found.
1 parent 58b2df6 commit f05c1b9

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

engine/tg_helpers/form_helper.php

+17-9
Original file line numberDiff line numberDiff line change
@@ -411,35 +411,43 @@ function form_file_select(string $name, ?array $attributes = null, ?string $addi
411411
* It can optionally clean up the retrieved value by trimming whitespace and
412412
* applying HTML special character encoding.
413413
*
414-
* @param string $field_name The name of the POST field to retrieve.
414+
* @param string $field_name The name of the POST field to retrieve, supports dot notation for nested fields.
415415
* @param bool $clean_up Whether to clean up the retrieved value (default is false).
416416
*
417-
* @return string|int|float|array|null The value retrieved from the POST data:
417+
* @return string|int|float|array The value retrieved from the POST data:
418418
* - string: for text inputs
419419
* - int: for integer values
420420
* - float: for decimal numbers
421421
* - array: for JSON objects or arrays
422-
* - null: if the field is not found
422+
* - empty string: if the field is not found
423423
*
424424
* @throws Exception If there's an error reading the input stream for JSON data.
425425
*/
426-
function post(string $field_name, bool $clean_up = false): string|int|float|array|null {
426+
function post(string $field_name, bool $clean_up = false): string|int|float|array {
427427
static $post_data = null;
428428

429429
if ($post_data === null) {
430430
$content_type = $_SERVER['CONTENT_TYPE'] ?? '';
431431
if (stripos($content_type, 'application/json') !== false) {
432432
$json_data = file_get_contents('php://input');
433-
$post_data = json_decode($json_data, true) ?? [];
433+
$post_data = json_decode($json_data, true);
434+
if (json_last_error() !== JSON_ERROR_NONE) {
435+
throw new Exception('Error decoding JSON data: ' . json_last_error_msg());
436+
}
434437
} else {
435438
$post_data = $_POST;
436439
}
437440
}
438441

439-
$value = $post_data[$field_name] ?? null;
440-
441-
if ($value === null) {
442-
return null;
442+
// Handle dot notation for nested fields
443+
$fields = explode('.', $field_name);
444+
$value = $post_data;
445+
foreach ($fields as $field) {
446+
if (isset($value[$field])) {
447+
$value = $value[$field];
448+
} else {
449+
return '';
450+
}
443451
}
444452

445453
if ($clean_up) {

0 commit comments

Comments
 (0)