Form Guard — Security

Write validation rules that accept legit input and reject XSS or SQLi: 18 levels of input validation, allowlists vs blocklists and constraints.

What you learn in this game

Never trust data from the browser: a page can be edited, a field removed. Laravel checks data on the server with $request->validate([...]): one line of rules per field, the rules split by |. Tap a field in the book, then tap rules in your hand.

Size rules (min, max, between, size) read a field as a number or as text. Laravel chooses from the field's other rules: with integer or numeric it measures the value; without them, it counts the characters.

A pick list on the page is no guard: anyone can send another value. On the server, name the values that may pass with in:, and everything else is refused.

XSS happens when text from a user is run by the browser as HTML. The defence is at output: escaping. Validation only checks the shape of data, and refusing < in a free text field refuses honest people too.

Laravel runs two middleware before validation: TrimStrings trims the spaces at the ends, and ConvertEmptyStringsToNull turns an empty field into null. An optional field needs nullable so that null is skipped.