Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

forms-validation

Form validation patterns

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Form validation patterns: the syntax, APIs, and habits you need before advancing in PHP.

The ORM is Django’s core productivity lever—N+1 queries and migration mistakes show up in every senior review.

You will apply Form validation patterns in contexts like: Contact forms, registration, and settings pages posting back to PHP scripts.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Never trust client-side validation alone. Server-side validation is authoritative—bots bypass JavaScript.

Validation checklist

  1. Read input from the correct superglobal
  2. Normalize (trim strings, cast numbers)
  3. Validate format and business rules
  4. Collect errors per field
  5. Re-render form with errors or process on success

Example rules

  • Required fields present and non-empty after trim
  • Email, URL, int ranges via filter_var
  • Enum-like values whitelisted (in_array($role, ['user','admin'], true))
  • CSRF token on state-changing POST (frameworks provide helpers)

Error array pattern

$errors = [];
if ($email === false) {
    $errors['email'] = 'Invalid email address.';
}

Self-check

  1. Why whitelist allowed roles instead of blocking bad ones?
  2. Where should validation run—browser or server?

Tip: Collect field errors in an associative array and re-render the form—users fix all issues in one pass.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Server validate why?
  • CSRF token where?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump