#Feedback on my multistep form logic and advice how to deal with images

32 messages Β· Page 1 of 1 (latest)

dim pecan
#

Hi guys,

I would like to have your feedback on this code and separation and dealing with this logic... I am learning and I am trying to make an event management system, where event is created out of 4 step form: overview, schedules, speakers, faq
Overview contains a thumbnail and each speaker has its own photo...

I will show EventService, EventRequest, and also store method in my controller (this isnt complete and I know I need to pass back the data as props so it would persist in the form)

I will leave my EventRequest and my store method below

#
    public function store(EventRequest $request): RedirectResponse
    {
        $user = auth()->user();
        $step = $request->get('step');
        $final = $request->boolean('final', false); // assuming you're passing this flag

        // If the image is uploaded, handle it before anything else
//        $overview = $request->safe()->except(['image']);
//
//        if ($request->hasFile('image')) {
//            $path = $request->file('image')->store('events', 'public');
//            $overview['image_path'] = $path;
//        }

        // Initialize the service
        $service = new EventService(
            user: $user,
        );

        // Inject the step data
        $service->step($step, $request->validated())->finalize($final);

        // Either save to PendingEvent or finalize to full Event
        if ($final) {
            return redirect()->route('events.index');
        }

        return redirect()->back();
    }```
idle pawn
dim pecan
idle pawn
#

What if the user fills out the whole form, and you then create the event. The user then decides actually one section of it was completly wrong and needs re-visitng, so they want to delete the data from it, and enter it at a later time. How would you apprach this? Recreate the pending model? Make those fields nullable in the event model? Prevent user from editing?

dim pecan
# idle pawn What if the user fills out the whole form, and you then create the event. The us...

I am not understanding it very clear, they would just edit the event? No? But then if they edit the form dont I have a problem there if they work straight with the event model? If they go to the next step the previous step will be saved and it that will be visible right away. Which means I would have to create another model instance for the edit to stop this? Idk if I explained the use case well

#

Thank you, but this is just simple for portfolio, nothing much... Apart from the logic is the code well separated?

dim pecan
idle pawn
#

What problem is there with image?

dim pecan
# idle pawn What problem is there with image?

Each event has a thumbnail and each speaker has its own image, but in order to persist those images (even if its a draft) I need to save it in my storage, where do I deal with this? Inside the controller or inside the service? Speakers is an array

dim pecan
# idle pawn Do it in the service

One more question, when user edits the form, should I check whether the model is dirty and then just do the update or update it always?

idle pawn
dim pecan
# idle pawn Just update it. Keep it simple. πŸ™‚

Since I am going with this approach with updating the event status, I am thinking of removing properties like $overview, faqs, speakers and schedules, and just pass $data to my saveStep() from which I will do a match and call a certain method depending on the step, only properties I would have is $user and $event, would that be a valid approach? Sorry for bothering, but you got more experience than me

idle pawn
dim pecan
#

I think you forgot to mention key mistake in my logic, I forgot to pass the created event back and forth so I can connect everything to the event thats drafted xD

idle pawn
dim pecan
#

Well I asked for feedback, but you have provided enough help, thanks

wintry blaze
#

I agree that it would ne better to pass a DTO to the service in stead of an array. It gives you type safety and makes the code easier to read. It also makes it easier to use the service from somewhere else. I would recommend you use Larastan to analyze your project. If you use level 8 or above, you'll quickly find out why passing arrays around is a bad idea πŸ˜†

dim pecan
#

And what do you mean use Service from somewhere else?

idle pawn
idle pawn
dim pecan
idle pawn
# dim pecan Like each method I have now in my service? I do not have a very clear idea what ...

I mean like this for example:

class FormData
{
    public function __construct(
        private ?string $stepAField = null,
        private ?string $stepBField = null,
        private ?string $stepCField = null,
    )
    {
    }

    public static function forStepA(array $data): self
    {
        return new self(
            Arr::get($data, 'stepAField'),
        );
    }
}

Request class:

    public function toDto(): ?FormData
    {
        $data = $this->all();
        
        return match ($this->get('step')) {
            'stepA' => FormData::forStepA($data),
            'stepB' => FormData::forStepB($data),
            default => null,
        };
    }

Controller:

    public function store(EventRequest $request): RedirectResponse
    {
        $dto = $request->toDto();

        // interact with service
    }
dim pecan
idle pawn
#

Just pass it to AI, he will solve it for you πŸ™‚

dim pecan