upnorthal's avatar

Project Flyer - Constraint - Max Number of

In the Project Flyer, say I would like to only allow a maximum of 6 photos to be associated with any flyer.

What would be the best way to implement this when a user attempts to upload new photos via the dropzone utility?

I don't believe this fits into the criteria of being an exception - as it is hardly an exceptional set of circumstances that a user could try to upload more than 6 photos.

Within the AddPhotoToFlyer class I've added a simple call to check that the count(*) of photos is less than 6 for the Flyer instance.

 protected function makePhoto()
    {
        // make sure max images not exceed


        if ($this->countPhotos() <6) {
        return new Photo(['name' => $this->makeFileName()]);
        }
        else {

//todo Return a nice message to the view

            abort(403,'Max photos of 6 reached ');
        }
    }



    protected function countPhotos()
    {
        return $this->flyer->photos()->count();  
    }

With the above, we just get a 403 response.

How can a flash message be passed back to the view to visually alert the user that a max of 6 images has been reached? A user isn't going to want to enable Chrome developer tools to view the response message.

0 likes
4 replies
upnorthal's avatar

Thanks fraserk, but that only constrains each POST to 6 photos. I need to make sure that there are never more than 6 rows in the Photos table associated with a single Flyer. (which they could do by returning to the form and submitting 6 each time).

Reading more into this, I'm assuming some kind of AJAX response could get fed back from the business logic to the view?

upnorthal's avatar

Okay, so

abort(403,'Max photos of 6 reached ');

returns a full on HTML template with my error stuck in the middle somewhere.

<!DOCTYPE html>
<html>
    
***snip
                    <div id="sf-resetcontent" class="sf-reset">
                <h1>Whoops, looks like something went wrong.</h1>
                                        <h2 class="block_exception clear_fix">
                            <span class="exception_counter">1/1</span>
                            <span class="exception_title"><abbr title="Symfony\Component\HttpKernel\Exception\HttpException">HttpException</abbr> in <a title="/home/vagrant/Code/ticketreg/vendor/laravel/framework/src/Illuminate/Foundation/Application.php line 882" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Application.php line 882</a>:</span>
                            <span class="exception_message">Max photos of 6 reached </span>
                        </h2>
***snip

You can see the full HTML above as a tooltip against the failed upload.

Do I need to somehow make custom error view, that returns just the error_message and code?

upnorthal's avatar

Having done some research and watching the Exceptions are like Bodyguards video, I created new custom Exception

<?php

namespace App\Exceptions;

class MaxPhotosReached extends \Exception
{


}

This is then referenced in the main Handler class (not shown in video currently)

class Handler extends ExceptionHandler
{
***snip

 public function render($request, Exception $e)
    {
        if ($e instanceof ModelNotFoundException) {
            $e = new NotFoundHttpException($e->getMessage(), $e);
        }

// Added new if statement below to cater for the new custom exception
        if ($e instanceof MaxPhotosReached) {

           return response($e->getMessage(),'400');
        }


        return parent::render($request, $e);
    }
}

finally, changed the domain login instead of using the abort, I now throw a new instances of my custom exception

//abort(403,'Max photos of 6 reached ');
         throw new MaxPhotosReached('max photos reached');

Not sure if the above is the best way of doing things, but it works. The Dropzone tooltip shows the text error without the HTML padding.

Please or to participate in this conversation.