phil / custom-fields
Let administrators add fields to records without code: typed values, kits per category, internal, permission-gated or secret fields, and safe formulas.
Package info
github.com/parler-haut-interagir-librement/custom-fields
pkg:composer/phil/custom-fields
Requires
- php: >=8.5
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ^13.4
Suggests
None
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-30 02:52:58 UTC
README
Let administrators add fields to records without code: typed values, kits per category, internal, permission-gated or secret fields, and safe formulas.
phil/custom-fields is a plain PHP library. An application describes the extra fields of a kind of record — a course, a learner, a product, an event — as data its administrators edit, and the library checks what people enter, computes the formulas and decides who sees each value. The values go to one JSON column; the definitions go to another, or to a file.
- Nine types: one-line and long text, whole and decimal numbers, booleans, dates, one choice or several, and formulas. Each type has its bounds (length, value, date, number of choices) and a default, both checked when the field is built.
- Kits per category: a kit groups fields that apply together, automatically to the records of the categories it names (and of the categories below them, as the application passes them), or on request to any record. A key brought twice is refused, naming the kit.
- Who sees what: a field is public, internal (the staff only, never in a public page, feed or export), restricted to a permission, or secret — written by the staff, never read back, kept apart from the other values for an encrypted store.
- Safe formulas: arithmetic, comparisons,
and,or,not,if,min,max,round,coalesce, and the aggregatessum,avg,min,maxandcountof related records. A formula runs no code, reads only numbers and booleans, is bounded in size and depth, never depends on itself, and never shows a value to people who may not see one of the values it reads. - Exact decimals: a decimal is kept as a string with exactly its scale of decimals, so that
12.50stays12.50. - Worker-safe and fast: the validator keeps nothing between calls; a record of a hundred fields with twenty chained formulas validates in about 0.2 ms, and in about 1.2 ms with the aggregates of a thousand related records.
- Versioned JSON: field sets and kits encode to JSON with their version; decoding checks every member, within a size and a depth limit.
Status
| Part | State |
|---|---|
| Fields, their types, bounds and defaults | Implemented and tested |
| Field sets, relations and the order of formulas | Implemented and tested |
| Kits applied by category or on request | Implemented and tested |
| Validation, records, stored values and secrets | Implemented and tested |
| Visibility and audiences | Implemented and tested |
| The formula language | Implemented and tested, fuzzed |
| The JSON codec of field sets and kits | Implemented and tested, fuzzed |
Documentation
| Document | What it covers |
|---|---|
| Installation | Requirements, installing, a first field set. |
| Fields | The types, their bounds and defaults, and how entered values are read. |
| Kits | Kits by category and on request, and the field set of one record. |
| Visibility | Public, internal, restricted and secret fields, audiences and permissions. |
| Formulas | The formula language, related records, and what a formula may read. |
| Storage | Stored values, secrets, and the JSON codec of definitions. |
| Extension points | What an application may replace or add, and what is stable. |
| Testing | The tests, the fuzz targets and the benchmark. |
| Reference | Every public type, with its role. |
Quick start
composer require phil/custom-fields
use Phil\CustomFields\Field; use Phil\CustomFields\FieldSet; use Phil\CustomFields\FieldType; use Phil\CustomFields\Validator; use Phil\CustomFields\Visibility; $fields = new FieldSet([ new Field('hours', FieldType::Decimal, 'Training hours', required: true, min: 0, scale: 1), new Field('level', FieldType::Choice, 'Level', choices: ['Beginner', 'Advanced'], default: 'Beginner'), new Field('rate', FieldType::Decimal, 'Hourly cost', visibility: Visibility::Internal), new Field('cost', FieldType::Formula, 'Cost', formula: 'hours * rate', visibility: Visibility::Internal), ]); $validation = new Validator()->validate($fields, $request->request->all('fields')); if (!$validation->isValid()) { return $form->showErrors($validation->violations); } $record = $validation->record(); $course->setFields($record->stored()); // ['hours' => '7.5', 'level' => 'Beginner', 'rate' => '80.00'] $record->get('cost'); // 600.0
Requirements
PHP 8.5 or later with ext-mbstring.
Contributing
Contributions are welcome on the PHIL monorepo: read the contribution guide and the code of conduct. Report a vulnerability privately, as the security policy explains, never in a public issue.
License
MIT, © Philippe Gamache, doing business as Le Gnome Archiviste; a Parler Haut Interagir Librement project. See LICENSE.