Skip to content

Conversation

@iamgerwin
Copy link

Summary

This PR adds proper support for Nova's dependsOn functionality inside flexible content layouts by introducing a new middleware that transforms field names in dependent field requests.

Problem

When using dependsOn on fields inside a Flexible layout, the callbacks don't receive the expected field names. This is because:

  1. Flexible content prefixes field names with a unique group key (e.g., ckOi67sYIRR0L6kh__product)
  2. When Nova makes AJAX requests to creation-fields or update-fields endpoints for dependent fields, it sends these prefixed names
  3. The dependsOn callback expects the original field names (e.g., product)

Solution

Added a new middleware InterceptFlexibleDependsOnAttributes that:

  • Intercepts Nova's dependent field API requests (creation-fields and update-fields)
  • Detects if the request contains flexible content prefixed field names
  • Transforms the field query parameter by stripping the group prefix
  • Transforms input field names by stripping the group prefix

This allows the dependsOn callbacks to work correctly inside flexible layouts.

Usage Example

Flexible::make('Order items')
    ->addLayout('Select Products', 'product', [
        Select::make('Product')
            ->options(Product::all()->pluck('name', 'id'))
            ->displayUsingLabels()
            ->searchable(),
        Text::make('Price')
            ->dependsOn(['product'], function (Text $field, NovaRequest $request, FormData $formData) {
                if ($formData->product) {
                    $product = Product::find($formData->product);
                    $field->withMeta(['value' => $product->price]);
                }
            }),
    ])

Files Changed

  • src/Http/Middleware/InterceptFlexibleDependsOnAttributes.php - New middleware for handling dependsOn requests
  • src/FieldServiceProvider.php - Register the new middleware

Technical Details

The middleware uses the existing FlexibleAttribute::GROUP_SEPARATOR constant (__) to detect and strip group prefixes from field names. It only processes requests that match Nova's dependent field API routes.

Fixes #524

This commit adds a new middleware (InterceptFlexibleDependsOnAttributes)
that transforms flexible content field names in dependsOn requests by
stripping the group key prefix. This allows the dependsOn callbacks
to receive the original field names they expect.

The middleware intercepts Nova's creation-fields and update-fields
API requests and:
- Transforms the 'field' query parameter by removing the group prefix
- Transforms input field names by removing the group prefix

This fixes the issue where dependsOn callbacks inside flexible layouts
receive prefixed field names (e.g., 'ckOi67sYIRR0L6kh__product')
instead of the expected original names (e.g., 'product').

Fixes whitecube#524
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The dependsOn is not working in the field inside Flexible

1 participant