CodeSleeve / stapler

ORM-based file upload package for php.
http://codesleeve.com
Other
538 stars 144 forks source link

Override Default Error Messages? #132

Closed chrisatomix closed 9 years ago

chrisatomix commented 9 years ago

I'm currently having issues with users uploading invalid images on one of my sites, despite explaining the requirements on the page. Unfortunately I can't see any way to override the hardcoded error strings returned by getErrorMessage() in my custom Exception handler. The Exception thrown doesn't include the error code, just the message text itself:

public function validate()
    {
        if (!$this->isValid()) {
            throw new FileException($this->getErrorMessage());
        }
    }

The message itself isn't very user-friendly, and I don't see any obvious way to change the text to something more appropriate to my end-users:

"The file "this-is-not-an-image.pdf" exceeds the upload limit defined in your form."

I also can't seem to access the UploadedFile instance from the Exception, which means I can't access the error/filename/filesize etc directly to customize the error message.

I'm using Laravel 4.2, but I believe this question is more directly related to stapler itself than laravel-stapler.

My current workaround is to wrap the save() call in a try/catch in the controller, then I can access the Eloquent model and the associated data to display more helpful error messages.

try {
  // Try saving the User, returns false on failure or throws Exception if photo upload fails
  $success = $user->save();
}
catch (Exception $e) {
  // Dangerously catch all exceptions, append message to validation errors list
  switch(get_class($e)) {
    case "Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException":
      // AccessDeniedExceptions occur when a user uploads an invalid image
      $user->validationErrors->add('photo',"Please upload a valid Image (JPEG, PNG, GIF). You may need to resave your file before uploading it.");
      break;
    default:
      // This could include standard Stapler FileExceptions, such as files too large etc
      // NOTE: This code arrogantly assumes that any Exceptions will be directly related to the photo field, which may not be the case
      // Here we can stristr() the $e->getMessage() and replace the text with something nicer
      $user->validationErrors->add('photo',$e->getMessage()); // Add error message to validator array
      break;
  }
  $success = false; // Saving failed
}

if($success) {
  // Success
  $msgtype = 'success';
  $msg     = 'User Photo has been Uploaded';
  return Redirect::to("user/photo")->with($msgtype, $msg);
}
else {
  // Failed
  $msg     = 'User Photo could not be Uploaded';
  $msgtype = 'error';
  $errors  = $user->errors();

  return Redirect::to("user/photo")->withErrors($errors)->withInput()->with($msgtype, $msg);
}

Anyway, I've worked around this now in my own ugly way, but it's still something that I think needs to be considered for a future version.

tabennett commented 9 years ago

I would validate that the uploaded file is an actual image before trying to save the model. Those validation errors you're seeing are part of the Symfony Uploaded File object. Anyways, glad you got it working!