DEV Community

Yasser Elgammal
Yasser Elgammal

Posted on

1

Laravel Validation: Tips and Tricks for Building Secure Applications

If you are taking any data from the user, pay attention to validations.

Unfortunately, I see some tutorials that takes data directly from the user, which allows the user to pass any inappropriate data or even guess the names of the database tables and send data to other fields that are not in the form.

Therefore, pay attention to the following:

1- Start with HTML... Your validations should start from the first HTML.
You should make sure that the required fields are marked as "required".

These required fields should be validated on the server-side but if the user leaves them empty... Why waste server requests on empty fields?

2- If you have MultiSelect/Select or Hidden fields, It's important to validate the value entered in them because it is easy for the user to inspect and change the data sent to the database.

3- Prevent Mass assignment, in laravel you can use one of these into your model ( fillable or guarded), The guarded property specifies which attributes should not be mass assignable, while the fillable property specifies which attributes are allowed to be mass assignable."

4- It's very important to validate any type of image or file that will be uploaded.

5- Do not use $request->all() and take the validated data.

You are dealing with all kinds of users, so expect the worst.

If you have read this far, thank you for your time ❤️

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay