wintercms / wintercms/winter

hasOne relation in NestedForm

Open
#1,414 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.5k
Forks
246
Avg merge
19h 2m
Merged PRs (30d)
7

Description

Winter CMS Build

dev-develop

PHP Version

8.4

Database engine

MySQL/MariaDB

Plugins installed

No response

Issue description

The type: relationmanager has an 'update' button you have to click before you can edit the model.
Which is why I want to render my relation in a nestedform. That way you can edit it straight away from the main model without having to click on the update button and edit the record in a popup.

Case 1:
The issue I'm encountering is that the nestedform is not being filled in.
I can fill in data manually and save the main record. This creates/updates a leadtracker record.
But the data is never displayed when loading in the page.

Case 2:
To solve the displaying issue, I edited my fields.yaml but now the nestedform does not save/create/update anymore.

Steps to replicate

Case 1: Nestedform is not being filled in

public function boot()
    {
        UserModel::extend(function ($model) {

            // add relation
            $model->hasOne['leadtracker'] = ['Aic\LeadTracker\Models\Lead'];

            // // fill in the fields (because we use nestedForm instead of relationManager)
            // $model->bindEvent('model.form.filterFields', function ($formWidget, $fields, $context) use ($model) {

            //     $lead = $model->leadtracker;
            //     dd($fields->leadtracker);
            //     if ($lead) $fields->leadtracker->value = $lead->getAttributes();

            // });

        });

        UsersController::extendFormFields(function ($form, $model, $context) {

            // return if...
            if ($form->isNested) return;
            if (!$model instanceof UserModel) return;
            if (!$model->exists) return;
            if ($form->getContext() == 'create') return;
            
            // add fields
            $form->addTabFields([
                '_leadtracker_fields' => [
                    'tab' => 'aic.leadtracker::lang.title',
                    'type' => 'nestedform',
                    'usePanelStyles' => false,
                    'form' => '~/plugins/aic/leadtracker/models/lead/fields.yaml'
                ]
            ]);

            // // works but there is an "update" button
            // $form->addTabFields([
            //     'leadtracker' => [
            //         'tab' => 'aic.leadtracker::lang.title',
            //         'type' => 'relationmanager',
            //     ]
            // ]);
        });
    }
fields:
    from:
            label: aic.leadtracker::lang.from.title
            type: balloon-selector
            options: aic.leadtracker::lang.from.options
            default: 'kleurstalen'
            span: storm
            cssClass: col-xs-6
    log:
            type: repeater
            titleFrom: contacted_at
            style: collapsed
            span: storm
            cssClass: col-xs-12
            form:
                fields:
                    type:
                        label: aic.leadtracker::lang.log.type.title
                        type: balloon-selector
                        options: aic.leadtracker::lang.log.type.options
                        default: 'Call'
                        span: storm
                        cssClass: col-xs-12
                    ...
                    ...

Case 2: Nestedform is filled in but does not create/save anymore

$form->addTabFields([
    '_leadtracker' => [
        'tab' => 'aic.leadtracker::lang.title',
        'type' => 'nestedform',
        'usePanelStyles' => false,
        'form' => '~/plugins/aic/leadtracker/models/lead/fields_nestedform.yaml'
    ]
]);
fields:

    leadtracker[from]:
        label: aic.leadtracker::lang.from.title
        type: balloon-selector
        options: aic.leadtracker::lang.from.options
        default: 'kleurstalen'
        span: storm
        cssClass: col-xs-6
      ...
      ...

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the UsersController::extendFormFields entry point and compare the nestedform configurations in fields.yaml and fields_nestedform.yaml with the working relationmanager configuration. Trace how the hasOne leadtracker relation is loaded and persisted; done means existing leadtracker data is displayed and nestedform edits create, update, and save correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
mariadb, mysql, php
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.