Defining Your First Custom Field
A focused walkthrough of a single field: change its type, validate its input, then lock it down for editing.
1. Change the type
Start with ->change_type($field, $type, $length = '', $extra = []) to pick how the field renders in the form:
$xcrud->change_type('email', 'text');
See Field Types Overview for the full list of available types.
2. Validate its value
->validation_pattern($field, $pattern) requires a field's (non-empty) value to match a regex in the Add/Edit form, enforced both client- and server-side. $pattern is either one of the named shortcuts below, or a literal regex body with no delimiters:
$xcrud->validation_pattern('email', 'email');
| 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 |
For anything else, pass a literal regex body (no slashes, no flags):
$xcrud->validation_pattern('sku', '^[A-Z]{2}-\d{4}$');
An empty field passes a pattern check on its own - pair it with ->validation_required() to also forbid leaving it blank. See Field Validation for the full required/pattern write-up.
3. Show it, but block editing
->readonly($field) and ->disabled($field) both display a field in the form while preventing the visitor from changing it - the difference is purely cosmetic (a readonly-styled input vs. a grayed-out disabled control). Neither is a server-side security control: they only change what the browser lets a visitor click, not what the API accepts.
$xcrud->readonly('accountNumber');
A visitor who calls the REST API directly, bypassing the form entirely, is not stopped by readonly()/disabled() at all. The real security boundary is whitelisting which columns are editable in the first place - see Security: Blacklisting Tables & Columns.
Putting it together
$xcrud = Xcrud::get_instance();
$xcrud->table('customers');
$xcrud->route('customers');
$xcrud->change_type('email', 'text');
$xcrud->validation_pattern('email', 'email');
$xcrud->readonly('accountNumber');
$xcrud->render();