Form Factory - Implementation
Building the renderer, and the decisions that decide whether it survives
Post 24 covered why. This one’s about building the thing without painting yourself into a corner.
Step by step
1. Design the schema first
Everything downstream is shaped by this, so spend time on it before writing a renderer.
Model it as a form with an ordered list of fields, where each field carries its type, label, validation rules and display conditions. Version the schema from day one - you will change it, and forms created under the old shape need to keep working.
Store it where it belongs: Dataverse tables if business users will edit it, JSON web files if only developers touch it. Dataverse costs more effort up front and pays it back the first time someone changes a label without asking you.
2. Build the renderer
One function, definition in, DOM out. Keep it dumb - it looks up a field type, finds the matching renderer, and calls it.
Register field types rather than switching on them. A registry means adding a type is adding an entry, not editing the core. That’s the difference between a factory that stays clean and one that accumulates special cases.
Render progressively: get plain fields working end to end before adding conditional logic, then validation, then the fancy types. Each layer should work without the ones above it.
3. Validation on both sides
The definition drives client-side validation, which is a courtesy for the user. The server has to enforce the same rules independently, because anything driven by client configuration can be edited by the client.
Derive both from the same definition if you can - one source, two enforcers. Rules that drift apart produce the worst kind of bug: forms that submit successfully and then fail silently in the backend.
4. Conditional logic
Keep the expression language small. “Show this field when that field equals a value” covers most requirements. The moment you need arbitrary expressions, ask whether that form should be hand-built instead.
Evaluate conditions on every relevant change, and make sure hidden fields are excluded from validation and from submission. Half the bugs in generated forms are a hidden required field blocking a submit nobody can see.
5. Submission
Map the definition to your target entity, then submit through the same path a hand-built form would use. Don’t invent a second write path - you’ll only have to secure it twice.
Handle partial failures. If the form writes to more than one entity, decide what happens when the second write fails, and make sure the user finds out.
How to make a mess and fix it
A form renders blank. Your definition failed to parse or a field type isn’t registered. Fail loudly in development rather than rendering nothing.
Validation passes on screen and fails on save. Client and server rules have drifted apart. Same source, both sides.
A hidden field blocks submission. Exclude anything not displayed from validation.
Nobody can debug a form. Add a way to dump the resolved definition and current state. You will need it, and so will whoever comes next.
Wrapping up
The whole thing lives or dies on the schema and the field type registry. Get those right, keep the renderer boring, enforce validation server-side, and give yourself a debugging affordance before you need one.
Useful links
- Add custom JavaScript to a page
- Web API overview
- Web API write, update and delete operations
- Web files, for JSON definitions and scripts
- Basic forms reference
Next: custom table components, for when the built-in list stops being enough.