Form Validation for File Uploads
Let's learn how to validate file uploads.
We'll cover the following
Checking if the user has uploaded a picture
So far, providing a picture was optional. What if we want to force the user to upload a picture? We will have to cover two scenarios:
- When the user is creating a new tour, we should verify that the user has uploaded a picture.
- When the user is editing a tour, we should verify that the user has uploaded a picture before or that they are uploading a picture now.
Instead of implementing a similar check in two different files, let’s make this part of the validation function we already have in tour-crud.php
.
If $normalizedData['picture']
is undefined, it means that the user didn’t previously upload a picture, nor are they in the process of uploading one now.
So we can cover both scenarios with one if
statement:
Get hands-on with 1400+ tech skills courses.