CodeBlanks Logo Icon CodeBlanks

Validating data

PHP course · Intermediate · Lesson 96/115

Write validation functions that check for empty fields and email format.

Why validation is needed

When the user submits the form, fields may be empty or the email format may be wrong. Without server-side validation, junk data gets processed; emails fail to send or the database gets dirty.

You make two core checks: empty() tells you whether a field is blank; filter_var($email, FILTER_VALIDATE_EMAIL) checks whether the email address has a valid format. You collect errors in an $errors array and proceed only when it stays empty.

The validateMessage function

The validateMessage(ContactMessage $m): array function takes a ContactMessage instance and returns an array of errors. You start with $errors = [];. When empty($m->name) you append $errors[] = "Name empty";.

For email you join two conditions with ||: empty($m->email) || filter_var($m->email, FILTER_VALIDATE_EMAIL) === false. This catches both an empty value and an invalid format in one branch. Then you return $errors; to hand the errors back.

Exercise Write the validateMessage function

Write a validateMessage(ContactMessage $m): array function. Inside, start with $errors = [];. When empty($m->name) append $errors[] = "Name empty";. When empty($m->email) || filter_var($m->email, FILTER_VALIDATE_EMAIL) === false append $errors[] = "Email invalid";. Then return $errors;. For testing, create $valid (valid name and email) and $invalid (empty name, invalid email) instances and write echo count(validateMessage($valid)) . "/" . count(validateMessage($invalid));.

Show example solution
array { $errors = []; if (empty($m->name)) { $errors[] = "Name empty"; } if (empty($m->email) || filter_var($m->email, FILTER_VALIDATE_EMAIL) === false) { $errors[] = "Email invalid"; } return $errors; }. Create $valid and $invalid instances, echo count(validateMessage($valid)) . "/" . count(validateMessage($invalid));.
Example solution:
<?php
// Add data validation
class ContactMessage {
    public string $name = "";
    public string $email = "";
    public string $message = "";
}

function renderForm(): string {
    return '<form method="post" action="/contact.php">'
        . '<label>Name: <input type="text" name="name"></label>'
        . '<label>Email: <input type="email" name="email"></label>'
        . '<label>Message: <textarea name="message"></textarea></label>'
        . '<input type="submit" value="Send">'
        . '</form>';
}

function validateMessage(ContactMessage $m): array {
    $errors = [];
    if (empty($m->name)) {
        $errors[] = "Name empty";
    }
    if (empty($m->email) || filter_var($m->email, FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = "Email invalid";
    }
    return $errors;
}

$valid = new ContactMessage();
$valid->name = "Alex";
$valid->email = "alex@example.com";
$invalid = new ContactMessage();
$invalid->name = "";
$invalid->email = "invalid";
echo count(validateMessage($valid)) . "/" . count(validateMessage($invalid));

Test yourself

  1. What does filter_var with FILTER_VALIDATE_EMAIL return for an invalid email?

    • Returns true
    • Returns the email string
    • Throws an exception
    • Returns false
    Show answer

    Returns false — Correct. For an invalid email filter_var returns false; for a valid one it returns the email string.

  2. Complete the filter_var call

    if (filter_var($email, ____) === false) {
        $errors[] = "Invalid email";
    }
    Show answer
    if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        $errors[] = "Invalid email";
    }
  3. Match the validation concepts

    empty()
    Empty field check
    filter_var
    Email check
    FILTER_VALIDATE_EMAIL
    Email filter
    $errors
    Error list

All lessons in Intermediate