/

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);
ParameterTypeDefaultDescription
$fieldstring-Field that must be non-empty to save
$minLength?intnullWhen 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}$');
ParameterTypeDefaultDescription
$fieldstring-Field to validate
$patternstring-A named shortcut, or a literal regex body with no delimiters
Named shortcutMatches
emailA simple user@host shape
alphaLetters only
alpha_numericLetters and digits
alpha_dashLetters, digits, underscore, hyphen
numericAn integer or decimal number, optionally negative
naturalDigits only (a non-negative integer)
urlAn 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();