When should you create a new form request in Laravel?

Do you use Laravel’s Form Request object to validate user input, but even though the forms are very similar, different requests, like updates and creates are just slightly different?

Maybe fields that are required in a create route aren’t required to store an update? Maybe a database lookup is needed for existing records that aren’t necessary when creating from scratch?

Since the validation rules in a form request are in a rules method it can be tempting to add some conditionals to your rules for “minor” tweaks instead of creating another form request class.

I think you should create a new form request for every new set of validation rules. The reason is simple:

It’s easier to read a declaration of rules than to think through conditionals!

When you come back to change this in a month or in a year, you’ll thank yourself for having multiple classes, even if it seems cluttered in your directory structure, because you won’t have the added mental overhead of various if statements or ternaries.

So next time you realize your update and create user routes have slightly different rules for validation, go ahead and create a UserUpdateRequest and UserCreateRequest and don’t worry about over-duplication here.

writing

I write about technology I'm using and the tech ecosystem.

speaking

I speak at tech and other events. You can see the recordings/slides.

resume

Here's my work history.

contact me

If you're interested in working together, let me know!