- Add resolve_constraints_in_content() to handle ${constraint.path} patterns
- Integrate into all form loading functions (load_from_file, load_fragment_form, etc)
- Support nested path navigation (e.g., constraints.tracker.udp.max_items)
- Add test_constraint_interpolation() test
fix(nickel-roundtrip): apply constraint interpolation in roundtrip workflow
- Fix execute_form() to use load_from_file() instead of parse_toml()
- Ensures constraints are resolved in roundtrip mode
docs(examples): add constraint interpolation example
- Create examples/05-fragments/constraints.toml
- Update examples/05-fragments/array-trackers.toml to use ${constraint.*}
- Document constraint workflow in examples/05-fragments/README.md
Benefits:
- Single source of truth for validation limits
- Forms auto-resolve constraints at load time
- All layers (Forms, Nickel, Templates) sync automatically
3.4 KiB
3.4 KiB
Form Fragments & Composition
Reusable form components, modular form building, and constraint management.
Files
Fragments (Reusable Components)
- header.toml - Header/intro section
- custom_border_section.toml - Styled section template
- fancy_border_section.toml - Advanced border styling
- employee_info_section.toml - Employee data fields
- agreement_section.toml - Legal/policy acceptance
- support_section.toml - Support tier selection
- premium_section.toml - Premium features section
- enterprise_section.toml - Enterprise tier section
Examples Using Fragments
- form_with_groups_includes.toml - Demonstrates fragment inclusion
- array-trackers.toml - RepeatingGroup arrays with constraint interpolation
Constraint Configuration
- constraints.toml - Single source of truth for array validation limits
Usage Pattern
Include Fragments in Your Forms
# main_form.toml
[includes]
header = "fragments/header.toml"
employee = "fragments/employee_info_section.toml"
agreement = "fragments/agreement_section.toml"
[sections.main]
title = "Employee Onboarding"
Build Complex Forms from Reusable Parts
# Each fragment can be tested independently
cargo run --example form_with_groups_includes
Constraint Interpolation Example
What is Constraint Interpolation?
Instead of hardcoding validation limits in forms, you can use constraint variables:
# array-trackers.toml
[[elements]]
name = "udp_trackers"
type = "repeatinggroup"
max_items = "${constraint.tracker.udp.max_items}" # ← Dynamic constraint reference
Single Source of Truth
All validation limits are defined in one file:
# constraints.toml
[tracker.udp]
max_items = 4 # Change this value, form auto-updates!
How It Works
- Form loads -
array-trackers.tomlcontains"${constraint.tracker.udp.max_items}" - Parser resolves - Form parser finds
constraints.tomlin same directory - Value injected - The constraint value (4) replaces the placeholder
- Form validates - User can add up to 4 UDP trackers
Try It
# Run the example
cargo run --example array-trackers
# The form will allow:
# - 0-4 UDP trackers (from constraints.tracker.udp.max_items)
# - 0-4 HTTP trackers (from constraints.tracker.http.max_items)
# Change constraints.toml:
# [tracker.udp]
# max_items = 6 # ← Change from 4 to 6
#
# Re-run the example - form now allows 0-6 UDP trackers!
Key Files
- constraints.toml - Defines validation limits
- array-trackers.toml - References constraints via
${constraint.path} - Form parser automatically resolves interpolations at load time
Benefits of Constraints
- DRY - Write common sections once
- Maintainability - Update fragments in one place
- Reusability - Share across multiple forms
- Modularity - Compose complex forms from simple parts
- Consistency - Unified styling and structure
- Constraint Sync - Single place to change validation limits (all layers auto-sync)
Fragment Library
Use these as templates for your own fragments:
| Fragment | Purpose | Fields |
|---|---|---|
| header | Form introduction | Title, description |
| employee_info | Standard employee data | Name, email, phone |
| agreement | Policy acceptance | Checkbox, text |
| support | Tier selection | Radio buttons |
| border_section | Custom styled container | Any fields |