Issue with nestedform when added dynamically

Hello,

I’m writing a plugin that add settings to the \Rainlab\User plugin.

I created my model, added a simple text field to it actually :

fields:
    primary_color:
        label: Primary color
        type: colorpicker
        availableColors: ['#2980b9', '#c0392b', '#16a085', '#7f8c8d', '#8e44ad', '#e67e22']

Now I want to add that form to my \Rainlab\User model. When I do that by hardcoding rainlab/user/models/user/fields.yaml directly, it works perfectly.

...
last_ip_address:
    label: rainlab.user::lang.user.last_ip_address
    span: auto
    disabled: true
    tab: rainlab.user::lang.user.account
    context: preview

settings:
    label: Settings
    type: nestedform
    form: '$/publipresse/giftcards/models/setting/fields.yaml'
...

But if I try to do it by extending the user backend form in my own plugin like this :

Event::listen('backend.form.extendFields', function($form) {
    if (!$form->getController() instanceof \Rainlab\User\Controllers\Users || !$form->getModel() instanceof \Rainlab\User\Models\User) {
        return;
    }

    $form->addTabFields([
        'settings' => [
            'label' => 'Settings',
            'tab' => 'Settings',
            'type' => 'nestedform',
            'form' => '$/publipresse/giftcards/models/setting/fields.yaml',
        ],
    ]);
});

The form loads indefinitely and ends up displaying a max execution time error.

If I change the field type to “text” like that :

Event::listen('backend.form.extendFields', function($form) {
    if (!$form->getController() instanceof \Rainlab\User\Controllers\Users || !$form->getModel() instanceof \Rainlab\User\Models\User) {
        return;
    }

    $form->addTabFields([
        'settings' => [
            'label' => 'Settings',
            'tab' => 'Settings',
            'type' => 'text',
            'form' => '$/publipresse/giftcards/models/setting/fields.yaml',
        ],
    ]);
});

It works and I can see my textfield added to my user backend form. The problem seems to be related to the nestedform field type, only when added using the extendFields event.

Did I miss something or it’s a real issue with nestedform ?

Thank you

I found the solution, post here as it can interest some people facing the same issue.

The problem came from that I didn’t define the relation between my user and my settings yet. It seems that, when nestedform hardcoded to the rainlab/user/models/fields.yaml file, it can deal with it, whereas when defined in the event, the relation is required in order to work.

1 Like

Glad you got it solved. Not sure why there is a difference in the extension method. A closer look at the code might be needed, especially since there is an infinite loop happening.