Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pabloasb's avatar

Display error message on a failed store attachments?

In a store attachment class with in an invokable that must return an array, how would I display the error if the file doesn't contain the information I need to work with? I would like to do something like Action::danger('Archive doesn't contain necessary files");, but that only works in actions when returned, not for store attachments.

0 likes
4 replies
pabloasb's avatar

@tykus That doesn't change the toast/error message displayed to the user. The error message still says "Server Error" and "There was a problem submitting the form.". I would like to change that so it displays "Archive doesn't contain necessary files"

pabloasb's avatar

@tykus I think I figured it out. I was looking for the above, because I wanted the verification of the files inside the zip archive to be a result of the transformation of the data in the store attachment class I created. But I created a validation rule object that opens the archive and validates the file types before it gets processed now by the store attachment. This is what my nova field looks like now:

File::make('Upload (zip)', 'file')
                ->rules([new FileZip]) // this is what i added for validation
                ->store(new MapFileStoreAttachment) // this is what is transforming and was pseudo-validating prior. 
                ->acceptedTypes('.zip')

Here's how my store attachment worked:

class MapFileStoreAttachment
{
    public function __invoke(NovaRequest $request, $model, $attribute, $requestAttribute, $disk, $storagePath)
    {
          // Extract the zip
          if (necessary files are present){
               // Convert data to $geometry
               return [
                                 'file' => $request->file,
                                 'map' => $geometry,
                            ];                                                    //store( callback ) only accepts arrays
          } else {
               // I wanted to throw an error toast here to tell the user to about the missing file types in the archive. 
               // If this was an action handle() I would return Action::danger("message");
               return ['file' => null]'; //store( callback ) only accepts arrays
         }
    }
}

I moved on from that idea and made FileZip to handle to validation

class FileZip implements ValidationRule
{
    //Determine if the validation rule passes.
    public function passes($attribute, $value)
    {
        // Accesses the zip archive and verifies that the required files are there
		returns boolean of the criteria
    }

    public function message(){ return 'The ZIP archive must contain required files.';}

    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (!$this->passes($attribute, $value)) {
            $fail($this->message());
        }
    }
}

This still shows the "There was a problem submitting the form" toast, but below the field in red it displays the message, which is good enough for my purposes.

However, if you do know how to change the toast or use it how I initially thought to use it in the store attachment, let me know.

Please or to participate in this conversation.