laravel / nova-issues

557 stars 34 forks source link

Repeater field content is not stored when submitted from parent resource form #5843

Closed tobias-grasse closed 1 year ago

tobias-grasse commented 1 year ago

Description:

Similar to the bug I reported in #5536, contents of a repeater field nested inside a parent resource form (User --hasOne--> UserProfile) are not properly saved when using the JSON preset. Probably because $attribute does not contain the nesting prefix like $requestAttribute does. I can work around this with a custom fillUsing callback, but it'd be nice to have this working out of the box. I assume the asHasMany preset has the same issue, but I did not test this.

Repeater field definition on my Nova UserProfile resource:

Repeater::make(__('Spoken Languages'), 'spoken_languages')
                ->repeatables([
                    SpokenLanguage::make(),
                ])
                ->nullable()
                ->asJson()

When I edit the UserProfile resource directly, the form content gets saved without issue. My current workaround is to slap this custom fillUsing callback on the field definition:

                // @FIXME: This is currently required to fill the repeater field from a parent resource form.
                ->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) {
                    $model->setAttribute($attribute, $request->input($requestAttribute));
                }),

Detailed steps to reproduce the issue on the provided Nova reproduction:

My reproduction defines a User --hasOne--> UserProfile relationship. The UserProfile model has an attribute extra_field which is cast to array and defined as a JSON column it the migration 2023_08_30_060437_add_extra_field_to_user_profiles_table. In app/Nova/UserProfile.php line 49, this attribute is mapped to a Nova Repeater field.

  1. Run artisan db:migrate --seed
  2. Login to Nova with test@example.com / password
  3. Create a user: /nova/resources/users/new
  4. Add one or more repeater field instances in the “Profile” section and fill them with values
  5. Click “Create User”
  6. On the detail view of the new user, click the edit icon for the user profile, ensure you are on the UserProfile edit route /nova/resources/user-profiles/<id>/edit
  7. On the user profile edit view, Repeater field is empty and database column user_profiles.extra_field is empty
  8. Set repeater field values again
  9. Save
  10. From the user detail view, edit the User again: /nova/resources/user/<id>/edit
  11. Repeater field is filled with the values from editing the UserProfile directly
  12. Add a repeater instance with random values
  13. Click “Update & continue editing”
  14. Both repeater field instances are gone, database is empty
crynobone commented 1 year ago

Please provide full reproducing repository based on fresh installation as suggested in the bug report template (or you can refer to https://github.com/nova-issues for example)

tobias-grasse commented 1 year ago

@crynobone I reused the repo for #5536 which already had the models set up, as I'm quite certain it is the same issue with nested forms. I can spin up a fresh reproduction, but the linked repo has no changes other than the barely necessary models + migrations.

github-actions[bot] commented 1 year ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.