Skip to content

Leaf Docs Show Incorrect Error Handling for Files #139

Description

@emajekral-collab

Hi again,

On this page: https://leafphp.dev/docs/utils/fs.html we see error reporting for file upload as:

$uploaded = request()->upload('fileToUpload', 'path/to/uploads');

if ($uploaded) {
  echo 'File uploaded successfully';
} else {
  $errors = request()->errors();
}

However, in Leaf v. 4.4 request()->upload() does file relocation with \Leaf\FS\File::upload() which logs errors to its own static::errorsArray. This errors array is not merged by request()->errors(). The code in \Leaf\Http::errors() shows:

    public static function errors()
    {
        return array_merge(
            static::$errors,
            static::$validator ? static::$validator->errors() : [],
            class_exists('\Leaf\Auth') ? static::auth()->errors() : []
        );
    }

This change fixes the issue for me:

    public static function errors()
    {
        return array_merge(
            static::$errors,
            static::$validator ? static::$validator->errors() : [],
            class_exists('\Leaf\Auth') ? static::auth()->errors() : [],
            \Leaf\FS\File::errors() // merges File system errors to handle errors from upload.
        );
    }

I'm having students merge the two errors arrays manually in their code like this:

$uploadOk = request()->upload(/*details, yadda*/);
if (!$uploadOk)
{
   $errors = array_merge($errors, request()->errors(), \Leaf\FS\File::errors()); // I'm having them add their own errors in the Controller as well for feedback in the View

It would be nice if either they didn't have to check the filesystem when they didn't appear to call it, or if the docs reflected the actual code. My test code from last January shows that I was doing the merge then, but I don't recall noticing it at the time. Possibly the documentation was incorrectly updated since then?

Thanks for your attention to this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions