Validation Patterns
Required Fields
Check whether a required text value is present.
required-field
Validation often starts by trimming input and checking that the required value is not empty.
Required Fields
required_field.php
Replay: real traced execution (multi-file project)
<?php
$name = "";
$trimmed = trim($name);
$status = $trimmed === "" ? "missing" : "ok";
echo "name=" . $name . "\n";
echo "status=" . $status . "\n";
<?php
$name = "Ada";
$trimmed = trim($name);
$status = $trimmed === "" ? "missing" : "ok";
echo "name=" . $name . "\n";
echo "status=" . $status . "\n";
<?php
$name = "Lin";
$trimmed = trim($name);
$status = $trimmed === "" ? "missing" : "ok";
echo "name=" . $name . "\n";
echo "status=" . $status . "\n";
$name ← (empty), $trimmed ← (empty), $status ← missing
1<?php2$name→ (empty) = ""; //@name="Ada", "Lin"3$trimmed→ (empty) = trim($name(empty));4$status→ missing = $trimmed(empty) === "" ? "missing" : "ok";56echo "name=" . $name(empty) . "\n";7echo "status=" . $statusmissing . "\n";outputname= status=missing
$name ← Ada, $trimmed ← Ada, $status ← ok
1<?php2$name→ Ada = "Ada";3$trimmed→ Ada = trim($nameAda);4$status→ ok = $trimmedAda === "" ? "missing" : "ok";56echo "name=" . $nameAda . "\n";7echo "status=" . $statusok . "\n";outputname=Ada status=ok
$name ← Lin, $trimmed ← Lin, $status ← ok
1<?php2$name→ Lin = "Lin";3$trimmed→ Lin = trim($nameLin);4$status→ ok = $trimmedLin === "" ? "missing" : "ok";56echo "name=" . $nameLin . "\n";7echo "status=" . $statusok . "\n";outputname=Lin status=ok