Field Validation
Require a field and check its shape - both enforced client-side for a smooth UX, and server-side because the client can never be trusted.
Required fields
->validation_required($field, $minLength = null) forbids saving the form with the field left blank, optionally also requiring a minimum length. Both checks run client-side (instant feedback) AND server-side (the actual enforcement).
$xcrud->validation_required('lastName');
$xcrud->validation_required('password', 8);
| Parameter | Type | Default | Description |
|---|---|---|---|
$field | string | - | Field that must be non-empty to save |
$minLength | ?int | null | When set, the value must also be at least this many characters |
Pattern validation
->validation_pattern($field, $pattern) requires a field's value (when non-empty) to match a regex. An empty field passes a pattern check on its own - pair with ->validation_required() to also forbid leaving it blank.
$xcrud->validation_pattern('email', 'email');
$xcrud->validation_pattern('sku', '^[A-Z]{2}-\d{4}$');
| Parameter | Type | Default | Description |
|---|---|---|---|
$field | string | - | Field to validate |
$pattern | string | - | A named shortcut, or a literal regex body with no delimiters |
| Named shortcut | Matches |
|---|---|
email | A simple user@host shape |
alpha | Letters only |
alpha_numeric | Letters and digits |
alpha_dash | Letters, digits, underscore, hyphen |
numeric | An integer or decimal number, optionally negative |
natural | Digits only (a non-negative integer) |
url | An http:// or https:// URL |
Why server-side enforcement matters
Client-side validation is a courtesy - it gives a visitor instant feedback without a round trip. It is not a security boundary: a client can bypass every client-side check simply by calling the REST API directly (e.g. with curl or fetch()), skipping the form and its JavaScript entirely. That's why ->validation_required() and ->validation_pattern() both re-run the exact same checks on the server before a row is ever written. See the REST API Reference for the raw endpoint these checks guard.
Putting it together
$xcrud = Xcrud::get_instance();
$xcrud->table('employees');
$xcrud->route('employees');
$xcrud->validation_required('lastName');
$xcrud->validation_required('firstName');
$xcrud->validation_pattern('email', 'email');
$xcrud->validation_pattern('extension', 'alpha_numeric');
$xcrud->render();