From 2ebc995ee2bfb3da5c21a93f401810eea2d2100a Mon Sep 17 00:00:00 2001 From: ralf Date: Thu, 23 Nov 2023 17:07:45 +0200 Subject: [PATCH] file upload report problem back to client-side, not just "server error" --- api/src/Etemplate/Widget.php | 14 ++++++++++++++ api/src/Etemplate/Widget/File.php | 1 + api/src/Etemplate/Widget/Vfs.php | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/api/src/Etemplate/Widget.php b/api/src/Etemplate/Widget.php index ddd1bb628f..0d69e04579 100644 --- a/api/src/Etemplate/Widget.php +++ b/api/src/Etemplate/Widget.php @@ -1029,6 +1029,20 @@ class Widget } } + /** + * Sets a validation error, to be displayed in the next exec + * + * @param string $name (complete) name of the widget causing the error + * @param string $cname =null set it to '', if the name is already a form-name, defaults to self::$name_vars + * @return string|null error-message already translated or null if there's none for the given $name + */ + public static function get_validation_errors($name, $cname=null) + { + if ($cname) $name = self::form_name($cname,$name); + + return self::$validation_errors[$name] ?? null; + } + /** * Check if we have not ignored validation errors * diff --git a/api/src/Etemplate/Widget/File.php b/api/src/Etemplate/Widget/File.php index 087400856b..cf564defb7 100644 --- a/api/src/Etemplate/Widget/File.php +++ b/api/src/Etemplate/Widget/File.php @@ -118,6 +118,7 @@ class File extends Etemplate\Widget { $widget = $template->getElementById($matches[1].':$cont[id]:'.$matches[3]); } + $mime = null; if($widget && $widget->attrs['mime']) { $mime = $widget->attrs['mime']; diff --git a/api/src/Etemplate/Widget/Vfs.php b/api/src/Etemplate/Widget/Vfs.php index a9d26c8ff7..beec32827f 100644 --- a/api/src/Etemplate/Widget/Vfs.php +++ b/api/src/Etemplate/Widget/Vfs.php @@ -145,7 +145,7 @@ class Vfs extends File protected static function process_uploaded_file($field, Array &$file, $mime, Array &$file_data) { parent::process_uploaded_file($field, $file, $mime, $file_data); - $path = self::store_file($_REQUEST['path'] ? $_REQUEST['path'] : $_REQUEST['widget_id'], $file); + $path = self::store_file($_REQUEST['path'] ?: $_REQUEST['widget_id'], $file); if($path) { $file_data[basename($file['tmp_name'])]['name'] = $file['name']; @@ -156,7 +156,7 @@ class Vfs extends File else { // Something happened with the VFS - $file_data[basename($file['tmp_name'])] = lang('Server error'); + $file_data[basename($file['tmp_name'])] = self::get_validation_errors($_REQUEST['widget_id']) ?? lang('Server error'); } }