Show fields conditionally and require values
Use field option recipes for live visibility and validation rules for server-side required values. Start with the complete offer example below, then add the small field-option change.
Complete example: acme_selection_fills_fields
Purpose (translated from upstream README)
Adapt (translated from upstream README)
Files relative to the system root:
├── README.md├── menu.bake.json├── modules/│ ├── acme_customers/│ │ ├── acme_customers.detail.json│ │ ├── acme_customers.index.json│ │ └── acme_customers.module.bake.json│ └── acme_offers/│ ├── acme_offers.detail.json│ ├── acme_offers.index.json│ └── acme_offers.module.bake.json├── preview/│ └── sample.bake.json├── tests/│ ├── acme_address_cleared.test.workflow.json│ └── acme_address_filled.test.workflow.json├── translations/│ ├── de.json│ ├── en.json│ └── translations.bake.json├── variants/│ └── client-on-change.json└── workflows/ └── acme_fill_offer_address.workflow.bake.jsonREADME.md
# acme_selection_fills_fields
Angebote / Offers. Kunde wählen: Der synchrone Server-Workflow übernimmt die Adresse ins bearbeitbare Formular; gespeichert wird erst beim Speichern. Leere Auswahl leert die Adresse. Ohne Berechnung oder zusätzliche Serverdaten ist die schnellere reine Client-Aktion empfohlen (siehe variants/client-on-change.json).
## Dateien
- `modules/`: Schema sowie Detail- und Index-Layouts.- `menu.bake.json`: Navigation (in ein vorhandenes Menü übernehmen).- `translations/`: deutsche/englische Labels und Bakery-Ressource.- `workflows/`, `tests/`: ausführbare Graphen und isolierte Business-Tests mit Fixtures.- `preview/`: optionale Bakery-Beispieldaten; nur in einer Vorschau anwenden.
## Anpassen
Alle `acme_*`-Modul-, Workflow-, Event- und Ressourcen-Identifier gemeinsam umbenennen; Referenzen, Layoutpfade, Filter, Übersetzungsschlüssel und Tests mitändern. Weitere Felder in Schema, Layout und beiden Sprachen ergänzen. Dateien relativ zur Systemwurzel kopieren. Native Module aus `fixtures/` nur ergänzen, wenn sie fehlen; bestehende Definitionen zusammenführen. Menüeinträge und Vorschaudaten sind optional. Varianten separat prüfen, nicht zusätzlich zur Hauptdefinition anwenden.
Prüfen: `php bakery plan <system>`, `php bakery apply <system>`, dann `php bakery system test-run <system>`. In diesem Repository führt `AcmeFeatureExamplesTest` dieselben Plan-/Apply-Schritte und die Workflow-Tests in der wegwerfbaren PHPUnit-Systemdatenbank aus. UI-Darstellung und reine Client-Aktionen sind damit nicht geprüft.menu.bake.json
[ { "resource_menu": "acme_selection_fills_fields", "resource": { "name": "ACME Inc.", "elements": [ "acme_customers", "acme_offers" ] } }]modules/acme_customers/acme_customers.detail.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "field_group", "identifier": "general", "options": { "fields": [ "name", "street", "zip", "city" ] } } ] } ] } ] } ]}modules/acme_customers/acme_customers.index.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "resource_table", "identifier": "records", "options": { "module": "acme_customers" } } ] } ] } ] } ]}modules/acme_customers/acme_customers.module.bake.json
[ { "resource_module": "acme_customers", "resource": { "identifier": "acme_customers", "title": "name", "fields": [ { "identifier": "name", "type": "text" }, { "identifier": "street", "type": "text" }, { "identifier": "zip", "type": "text" }, { "identifier": "city", "type": "text" } ], "layouts": { "detail": "/modules/acme_customers/acme_customers.detail", "index": "/modules/acme_customers/acme_customers.index" } } }]modules/acme_offers/acme_offers.detail.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "field_group", "identifier": "general", "options": { "fields": [ "name", "customer", "street", "zip", "city" ] } } ] } ] } ] } ]}modules/acme_offers/acme_offers.index.json
{ "tabs": [ { "identifier": "general", "rows": [ { "cols": [ { "span": 24, "components": [ { "type": "resource_table", "identifier": "records", "options": { "module": "acme_offers" } } ] } ] } ] } ]}modules/acme_offers/acme_offers.module.bake.json
[ { "resource_module": "acme_offers", "resource": { "identifier": "acme_offers", "title": "name", "fields": [ { "identifier": "name", "type": "text" }, { "identifier": "customer", "type": "select", "options": { "references": "acme_customers", "events": { "on_change": [ "acme_fill_offer_address" ] } } }, { "identifier": "street", "type": "text" }, { "identifier": "zip", "type": "text" }, { "identifier": "city", "type": "text" } ], "layouts": { "detail": "/modules/acme_offers/acme_offers.detail", "index": "/modules/acme_offers/acme_offers.index" } } }]preview/sample.bake.json
[ { "resource_entity": "acme_sample_customer", "resource": { "module": "acme_customers", "fields": { "name": "ACME Inc.", "street": "Example Street 1", "zip": "12345", "city": "Example City" } } }, { "resource_entity": "acme_sample_offer", "depends_on": "resource_entity.acme_sample_customer", "resource": { "module": "acme_offers", "fields": { "name": "ACME Offer", "customer": "${resource_entity.acme_sample_customer}" } } }]tests/acme_address_cleared.test.workflow.json
{ "identifier": "acme_address_cleared", "workflow": "acme_fill_offer_address", "fixtures": [ { "type": "entity", "module": "acme_customers", "alias": "customer", "fields": { "name": "ACME Inc.", "street": "Example Street 1", "zip": "12345", "city": "Example City" } }, { "type": "entity", "module": "acme_offers", "alias": "offer", "fields": { "name": "ACME Offer" } } ], "trigger": { "type": "dispatch_sync", "entry": "acme_fill_offer_address", "data": { "prototype": "fixture:offer", "selector": "$.customer", "value": null } }, "assertions": [ { "type": "response_json_path_equals", "path": "set.0.value", "value": "" }, { "type": "response_json_path_equals", "path": "set.1.value", "value": "" }, { "type": "response_json_path_equals", "path": "set.2.value", "value": "" }, { "type": "response_json_path_equals", "path": "save", "value": false } ]}tests/acme_address_filled.test.workflow.json
{ "identifier": "acme_address_filled", "workflow": "acme_fill_offer_address", "fixtures": [ { "type": "entity", "module": "acme_customers", "alias": "customer", "fields": { "name": "ACME Inc.", "street": "Example Street 1", "zip": "12345", "city": "Example City" } }, { "type": "entity", "module": "acme_offers", "alias": "offer", "fields": { "name": "ACME Offer", "customer": "fixture:customer" } } ], "trigger": { "type": "dispatch_sync", "entry": "acme_fill_offer_address", "data": { "prototype": "fixture:offer", "selector": "$.customer", "value": "fixture:customer" } }, "assertions": [ { "type": "response_json_path_equals", "path": "set.0.value", "value": "Example Street 1" }, { "type": "response_json_path_equals", "path": "set.1.value", "value": "12345" }, { "type": "response_json_path_equals", "path": "set.2.value", "value": "Example City" }, { "type": "response_json_path_equals", "path": "save", "value": false } ]}translations/de.json
{ "modules": { "acme_customers": { "title": "Kunden", "fields": { "name": "Name", "street": "Straße", "zip": "Postleitzahl", "city": "Ort" }, "tabs": { "general": "Allgemein" } }, "acme_offers": { "title": "Angebote", "fields": { "name": "Name", "customer": "Kunde", "street": "Straße", "zip": "Postleitzahl", "city": "Ort" }, "tabs": { "general": "Allgemein" } } }}translations/en.json
{ "modules": { "acme_customers": { "title": "Customers", "fields": { "name": "Name", "street": "Street", "zip": "Postal code", "city": "City" }, "tabs": { "general": "General" } }, "acme_offers": { "title": "Offers", "fields": { "name": "Name", "customer": "Customer", "street": "Street", "zip": "Postal code", "city": "City" }, "tabs": { "general": "General" } } }}translations/translations.bake.json
[ { "resource_translations": "de", "resource": { "identifier": "de", "code": "de_DE", "name": "Deutsch", "dir": "ltr", "provider": "deDE", "data": "${file('translations/de.json')}" } }, { "resource_translations": "en", "resource": { "identifier": "en", "code": "en_US", "name": "English", "dir": "ltr", "provider": "enUS", "data": "${file('translations/en.json')}" } }]variants/client-on-change.json
{ "events": { "on_change": [ { "action": "set", "fields": { "street": "this.customer ? this.customer.street : \"\"", "zip": "this.customer ? this.customer.zip : \"\"", "city": "this.customer ? this.customer.city : \"\"" } } ] }}workflows/acme_fill_offer_address.workflow.bake.json
[ { "resource_workflow": "acme_fill_offer_address", "resource": { "identifier": "acme_fill_offer_address", "title": "ACME Inc. — acme_fill_offer_address", "active": true, "async": false, "entry": "acme_fill_offer_address", "elements": [ { "name": "acme_fill_offer_address", "type": "event/on_change", "options": { "module": "acme_offers" }, "set": { "offer": "prototype:" }, "to": { "default": { "fill": [ "default" ] } } }, { "name": "fill", "type": "action/set", "options": { "recipes": { "street": "$offer.customer ? $offer.customer.street : \"\"", "zip": "$offer.customer ? $offer.customer.zip : \"\"", "city": "$offer.customer ? $offer.customer.city : \"\"" } }, "in": { "default": "$offer" }, "to": {} } ], "events": [ { "identifier": "acme_fill_offer_address", "type": "on_change", "module": "acme_offers" } ] } }]Source: brezel/api@572da72238ad1aab7b60287c898eaede2c08a876
Agents: system_authoring_command reference kind=feature query=acme_selection_fills_fields
How it works
Section titled “How it works”The example provides the customer selector, editable address fields and an on-change response. To hide the street input without a customer and require it when a customer is present, replace the street field definition as below. This extension is code-derived and has not been executed as a feature test.
{"identifier":"street","type":"text","options":{"recipes":{"hidden_from_frontend":"!this.customer"},"rules":"nullable|required_with:customer|max:191"}}For a fixed choice, use a rule such as required_if:status,approved. An explicit rules string replaces generated base validation, so include all required type/length rules.
Test it
Section titled “Test it”Copy the example files relative to your system root. Merge existing native modules, menus and roles; rename all related identifiers together. Apply preview data only in a disposable preview. Run the plan, apply and tests against that preview:
php bakery plan <system>php bakery apply <system>php bakery system test-run <system>The supplied tests cover address fill/clear, not visibility or the new rule. Check selecting and clearing in the browser, and submit empty/nonempty street values through REST with and without a customer. Add a validation regression case; field_hidden tests authorization, not SPA option recipes.
Other ways
Section titled “Other ways”- Recommended for server-data-dependent UI properties: synchronous
on_change→action/setwithproperties. Local option recipes avoid a round trip when all required data is already present. - Recommended for disabling an input:
options.recipes.readonly; retain server validation and field permissions where required. - Not recommended for authorization: hiding a field or button. Use role field permissions.
- Not recommended for repairing values on ordinary saves:
beforeSave+op/set; it does not persist those changes.
Reference
Section titled “Reference”Field options · Recipes by runtime · Field permissions · Workflow tests · Assertions