Form Validation for File Uploads

Let's learn how to validate file uploads.

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:

  1. When the user is creating a new tour, we should verify that the user has uploaded a picture.
  2. 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 1200+ tech skills courses.