yassilah / laravel-nova-nested-form

This package allows you to include your nested relationships' forms into a parent form.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Empty nested form on Detail view, hideFromDetail doesn't work

joseballester opened this issue · comments

When using NestedForm, a nested form appears in the Edit view (expected behavior) but it also appears, empty, in the Detail view:

Screen Shot 2019-08-30 at 13 34 28

As far as I understand, nested forms are useful to create/update related objects while editing. Therefore, I wouldn't expect having a nested form in the Detail view but using HasMany instead.

If I try to hide it from the Detail page with hideFromDetail() (according to Nova docs), the form disappears from both Edit and Detail views.

Hi @joseballester ,

Not really solving any issues here just to inform.

The reason hideFromDetail doesn't work is because when you do NestedForm::make(), that method actually creates and returns a NestedFormPanel which in turn proxies any method calls to the NestedForm.

The result is:

// $nestedFoosForm is a NestedFormPanel
$nestedFoosForm = NestedForm::make('Foo', 'foos');
// $nestedFoosForm is a NestedForm
$nestedFoosForm = $nestedFoosForm->showOnDetail(true);

You can use showOnDetail (and other methods) by storing the NestedForm in a variable, and call any methods before storing it in the fields array.

Example:

    public function fields(Request $request) {
        $nestedCustomerProductsForm = NestedForm::make('Product', 'customerProducts');
        $nestedCustomerProductsForm->showOnDetail(false);

        return [
            ID::make()->sortable(),
            Text::make('Name'),
            Text::make('Phone Number'),
            Text::make('Email'),
            $nestedCustomerProductsForm,
        ];
    }

Oh I see, thank you for your feedback!

Anyway I'd say that the default behavior should be either: (a) not being shown on Detail view and only on Edit view or (b) giving content to the NestedForm in the Detail view (now it appears empty). The pull request would implement (a) from my point of view.

However, it's true that I can just use your code as a workaround, without changing the package code itself. Thanks!