@@ -411,35 +411,43 @@ function form_file_select(string $name, ?array $attributes = null, ?string $addi
411
411
* It can optionally clean up the retrieved value by trimming whitespace and
412
412
* applying HTML special character encoding.
413
413
*
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 .
415
415
* @param bool $clean_up Whether to clean up the retrieved value (default is false).
416
416
*
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:
418
418
* - string: for text inputs
419
419
* - int: for integer values
420
420
* - float: for decimal numbers
421
421
* - array: for JSON objects or arrays
422
- * - null : if the field is not found
422
+ * - empty string : if the field is not found
423
423
*
424
424
* @throws Exception If there's an error reading the input stream for JSON data.
425
425
*/
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 {
427
427
static $ post_data = null ;
428
428
429
429
if ($ post_data === null ) {
430
430
$ content_type = $ _SERVER ['CONTENT_TYPE ' ] ?? '' ;
431
431
if (stripos ($ content_type , 'application/json ' ) !== false ) {
432
432
$ 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
+ }
434
437
} else {
435
438
$ post_data = $ _POST ;
436
439
}
437
440
}
438
441
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
+ }
443
451
}
444
452
445
453
if ($ clean_up ) {
0 commit comments