One Reason to ALWAYS use Form Requests for Validation

preview_player
Показать описание
I want to give you a strong argument on why validation logic in the controller is a violation of DRY and SOLID principles. Would you agree?

- - - - -
Support the channel by checking out our products:
Рекомендации по теме
Комментарии
Автор

Always be sure to balance design with simplicity.

While it is true that having inline validation violates SRP, I would argue that introducing a new class violates KISS for simple controllers. If you only have one action in the controller it would still be DRY without a separat form request class. If you add more actions and complexity to the controller, you should refactor - letting the design emerge because its required.

Asking "what if you add this and that later" is often referred to as YAGNI or a code smell called speculative generality.

rwpVfdpO
Автор

Solid advice. These days, I tend to use Form Request objects even when I only need to validate a single input field. It really keeps things clean and organized, while also providing a proper way to do some minor transformations with the input or things related to that particular form by overriding the prepareForValidation() method inside your Form Request object.

ОлегКнязев-жэ
Автор

finally someone say it to all juniors all over the world. Great start, waiting for next videos about solid

vovkko
Автор

I would argue that the consistency aspect of always using form requests is the selling point of itself + there could be a situation where we need some additional fields validated, therefore we already have a dedicated form request class where to put additional validation logic.

ward
Автор

Hi Laravel Daily, thanks for all your free high quality laravel tutorials on youtube. Your payable courses are short, simplified and on the point, too. It´s easy for me to follow you and I learned a lot since I know your channel. Thanks and go on bro!

tenariusstuff
Автор

The inline validation was one of the first things I noticed about Laravel and assumed it was the standard with Laravel.
Yii is very explicit where the validation rules should be and I preferred it
Thanks for sharing this.

boriakhi
Автор

Great advice, thanks! Can't wait for the SOLID one

malamhari_
Автор

And again...after watching one of your clips my todo list grows instantly :D

andreasbrunsmann
Автор

Congratulations for the 70k subscribers, you deserve it

josechirino
Автор

So, after Many tutorails on your channel, IT seems That Laravel daily is better resource than documentation.. my opinion!!

dinkodugec
Автор

Great advice & interesting aspect! I am currently using both in my project. One thing that sometimes prevents me from using form requests is the fact that form requests are done as the first thing in the controller function. Sometimes however, I need to do other things first in the controller, before validating the data that has been handed over.

coding_david
Автор

Soft, light, easy to understand, and contain great ideas
Really this my opinion about your videos
Wish you alll success 🌹🌹

yazankaake
Автор

Love it ! I'm always using Form Request, it helps to keep things clean and organized 😉

BenjaminHaeberli
Автор

Great video but just a quick comment.

SRP (Single Responsibility Principle) is not about how many things a class does (one or least as possible). SRP is about that class should have only one reason to be changed.

The example with mixing validation and creating demonstrates it very clear. When we have validation rules and then call `Product::create` now we have at least two reasons to change this class. First, when we change validation rules. Second, when we change what we call to store a product.

I always try to explain that validation is not a part of a "business logic". Yes, it should be executed before we create an object, but creational service should not care about any rules. It only needs to know how to create an object and maybe notify about the success or failure.

kinvain
Автор

I vote for FormRequest too. You can place also the access controll in that.

GergelyCsermely
Автор

Thank you as always. Keep going! Don't stop now!

belce
Автор

I always like to use the form request and also like the idea behind that vary much 👍😊

vatsalpatel
Автор

i tried Form Request Validation and it help also for authorizations as well .

ChangeYourLifeForever
Автор

Thank you for your advice. Looking forward to your solid video.

phamluong
Автор

Good argument! thank you! have a nice Sunday!

beatnu