nova-issues icon indicating copy to clipboard operation
nova-issues copied to clipboard

Undefined array key "type" on Repeater Field when using Octane in Vapor

Open msucevan opened this issue 1 year ago • 12 comments
trafficstars

  • Laravel Version: 10.45.1
  • Nova Version: 4.32.16
  • PHP Version: 8.3
  • Database Driver & Version:
  • Operating System and Version: AWS - Laravel Vapor

Description:

All the repeater fields are not working in Laravel Vapor.

Detailed steps to reproduce the issue on a fresh Nova installation:

Here is the implementation on one of them and the relative error:

{
"userId": 1,
"exception": {
  "class": "ErrorException",
  "message": "Undefined array key "type"",
  "code": 0,
  "file": "/var/task/vendor/laravel/nova/src/Fields/Repeater.php:241"
  },
  "aws_request_id": "feb6dff8-dd24-4529-be43-53247ad939ae"
}
Repeater implementation

Repeater::make(__('Values'), 'values')->repeatables([
    SettingsItem::make(__('Item'), 'item'),
])
    ->required()
    ->asJson(),
class SettingsItem extends Repeatable
{
    public function fields(NovaRequest $request): array
    {
        return [
            Text::make(__('Item'), 'item')
                ->rules('required'),
        ];
    }
}

This is just an example, I have over 10 Repeater fields spread around the project and everyone is failing for the same issue.

msucevan avatar Feb 23 '24 12:02 msucevan

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)

crynobone avatar Feb 23 '24 14:02 crynobone

Yesterday I've created a new fresh project and deployed to laravel Vapor and it works fine.

However, I have the same problem all around my project. In local is working perfectly without any kind of issue, when I'm my staging or production environment I occur in this problem.

Is really strange because the payload looks good.

image

    /**
     * Format available rules.
     *
     * @return array<array-key, mixed>
     *
     * @phpstan-return array<string, TFieldValidationRules>
     */
    protected function formatRules()
    {
        $request = app(NovaRequest::class);

        if ($request->method() === 'GET') {
            return [];
        }

        return collect($request->{$this->validationKey()})
            ->map(function ($item) {
                return $this->repeatables->findByKey($item['type']); // Is failing here
            })
            ->flatMap(function (Repeatable $repeatable, $index) use ($request) {
                return FieldCollection::make($repeatable->fields($request))
                    ->mapWithKeys(function (Field $field) use ($index) {
                        return ["{$this->validationKey()}.{$index}.fields.{$field->attribute}" => $field->rules];
                    });
            })
            ->all();
    }

This is just an example, I have over 10 Repeater fields spread around the project and everyone is failing for the same issue.

msucevan avatar Mar 05 '24 18:03 msucevan

After a few days under investigation, I realized that the problem is caused by Laravel-octane, but I have no idea why.

I will publish a reproducible repository soon @crynobone

msucevan avatar Mar 07 '24 17:03 msucevan

Please @crynobone you can fork the repo: https://github.com/msucevan/nova-issue

I will write an issue also in octane, maybe is more related with octane

msucevan avatar Mar 07 '24 18:03 msucevan

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 17 '24 16:03 stale[bot]

@crynobone any updates here? Should I provide more info?

msucevan avatar Mar 19 '24 10:03 msucevan

@msucevan I've run into a very similar issue with a nova library. Only occurs when running in the vapor environment with octane on.

https://github.com/ebess/advanced-nova-media-library/issues/369

numeralsix avatar Apr 23 '24 14:04 numeralsix

@msucevan I've run into a very similar issue with a nova library. Only occurs when running in the vapor environment with octane on.

ebess/advanced-nova-media-library#369

I'm not using this package. So you mean that removing this packages it works also with octane? Because in a fresh install is not working.

msucevan avatar Apr 23 '24 21:04 msucevan

Seems to be related to https://github.com/laravel/nova-issues/issues/5339#issuecomment-1437511009

Is everyone on the same free Vapor setup?

crynobone avatar May 07 '24 00:05 crynobone

@crynobone I tested using a custom domain and I have a validation error on an existing resource that don't occur when I'm not using Octane.

name: name
price: 110.0000
finishing_price: 
program_price: 
setup_price: 
factor: 1.2500
k_factors[0][type]: k-factor
k_factors[0][fields][r_t]: 1
k_factors[0][fields][k]: 2
k_factors[1][type]: k-factor
k_factors[1][fields][r_t]: 2.5
k_factors[1][fields][k]: 3.5
k_factors[2][type]: k-factor
k_factors[2][fields][r_t]: 2.5
k_factors[2][fields][k]: 2.5

"message": "The k factors.0.fields.r t field is required. (and 1 more error)",

If I send a resource where k_factors are empty or null I receive the error posted: "message": "Undefined array key "type"",

and this is the payload:

price: 110.0000
finishing_price: 
program_price: 
setup_price: 
factor: 1.2500
k_factors[0][type]: k-factor
k_factors[0][fields][r_t]: 1
k_factors[0][fields][k]: 2```

msucevan avatar May 13 '24 06:05 msucevan

CleanShot 2024-05-13 at 15 08 21

There's no r_t or k field based on the example repository

crynobone avatar May 13 '24 07:05 crynobone

Let's say that this answer is pretty boring and frustrating, since doesn't change which the field name is. Of course I will not spend time and money on this issue in a fresh repository (I already give you all the information and a fresh project). I tested the custom domains on my project, the issue is reproducible in the same way as I described in a fresh install on the code that I give you. If you just seed the data inside the fields we will have the same "validation" issue, come on........

msucevan avatar May 13 '24 07:05 msucevan