laravel / nova-issues

554 stars 35 forks source link

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

Open msucevan opened 6 months ago

msucevan commented 6 months ago

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.

crynobone commented 6 months 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)

msucevan commented 6 months ago

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 commented 6 months ago

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 commented 6 months ago

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

stale[bot] commented 5 months ago

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.

msucevan commented 5 months ago

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

numeralsix commented 4 months ago

@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

msucevan commented 4 months ago

@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.

crynobone commented 4 months ago

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

Is everyone on the same free Vapor setup?

msucevan commented 4 months ago

@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```
crynobone commented 4 months ago

CleanShot 2024-05-13 at 15 08 21

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

msucevan commented 4 months ago

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........