I have two models “race” and “result” with a has-one relationship defined.
I’m working on a backend feature where the admin can click on a “Create result” button from the parent model’s (race) detail page, to create a new result in a form where the parent race information populated in the relation manager (as it would be in the “update” view).
Despite passing the foreign key in the query string and setting it on the form model, the foreign key isn’t being set in the form. I’ve tried using formGetConfig, hidden fields, and the conditions property, but I’m still facing issues.
What I’ve tried:
Retrieving the foreign key from the query string.
Setting the foreign key on the form model.
Using the conditions property in the form configuration.
Any guidance on ensuring the foreign key is passed and used correctly in the form would be greatly appreciated!
I’ve only tried with the Relation Controller so far, relying on the built in relation manager that is rendered with the form controller and YAML configurations.
But from the documentation you’re referring to it seems like I need to create a custom relation manager for the “create” action, where I use the initRelation method to populate the form, and to set the foreign key (race_id) for the “Result”.
I got rid of the errors, and now it works as long as I’m using the relation widget, the foreign key ‘race_id’ is set and the widget is loaded as expected:
# fields.yaml for races — works, race_id is set!
result:
label: Result
nameFrom: title
type: relation
However, if I replace the widget with a nested form, the foreign key isn’t passed to the result form.
# fields.yaml for races — doesn't work, race_id isn't set :-/
result:
label: Result
type: nestedform
showPanel: false
form: $/linusbostrom/gasrundan/models/result/fields.yaml