Automatically populate parent model data when creating a new child item

Hi everyone,

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:

  1. Retrieving the foreign key from the query string.
  2. Setting the foreign key on the form model.
  3. 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!

Hi @linusb,

Are you doing with a Relation Controller or a custom solution ? (Relation Controller - October CMS - 3.x)

This would be the setup to follow for a “Has One” relation

1 Like

Thank you for your reply @apinard !

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”.

Hi @linusb,

You can download the OctoberCMS test plugin and see how it’s done.

Thanks @apinard for pointing me in the right direction — nested forms are of course the October way of addressing this problem.

I’m still getting errors, but that has probably to do with the relation controller.

Hi @linusb ,

What errors are you having?

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

Is there something I’m missing here?

Hi @linusb ,

Setting defaultCreate might help;

defaultCreate will create a new record when the form loads, useful for associating relations within the nested form

https://octobercms.com/docs/api/backend/formwidgets/nestedform

result:
    label: Result
    type: nestedform
    useRelation: true
    form: $/linusbostrom/gasrundan/models/result/fields.yaml
    defaultCreate: true

The drawback of this; it will always create new “result”, even if you don’t save a new Race form.

There must be a solution to it, I’ll take a look later when I have more time!