Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsTo field in custom actions not validated properly when parent resource is being searched #6529

Closed
shaffe-fr opened this issue Sep 13, 2024 · 2 comments

Comments

@shaffe-fr
Copy link

  • Laravel Version: 11.22.0
  • Nova Version: 4.35.1
  • PHP Version: 8.2
  • Database Driver & Version: mysql without Scout

Description:

If a BelongsTo field is used in a custom Action and if the parent resource is being searched, the Relatable validation rule fails because the parent search is applied to the validation query.

This occurs because BelongsTo creates the associatable query using the request search.

     public function getRules(NovaRequest $request)
    {
        $query = $this->buildAssociatableQuery( // <==
            $request, $request->{$this->attribute.'_trashed'} === 'true'
        )->toBase();

        return array_merge_recursive(parent::getRules($request), [
            $this->attribute => array_filter([
                $this->nullable ? 'nullable' : 'required',
                new Relatable($request, $query),
            ]),
        ]);
    }
    public function buildAssociatableQuery(NovaRequest $request, $withTrashed = false)
    {
        // ...
        $request->first === 'true'
                        ? $query->whereKey($model->newQueryWithoutScopes(), $request->current)
                        : $query->search(
                            $request, $model->newQuery(), $request->search, // <== here
                            [], [], TrashedStatus::fromBoolean($withTrashed)
                        );
    }

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

  1. Create a Product resource
class ProductResource extends Resource
{
    public $search = ['name'];
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make(),
            Text::make(__('fields.name'), 'name'),
            BelongsTo::make('Feature', 'feature', FeatureResource::class),
        ];
    }
    public function actions(NovaRequest $request): array
    {
        return [
            AssociateFeatureToProductAction::make()->onlyOnIndex(),
       ];
    }
}
  1. Create a Feature resource
class FeatureResourceextends Resource
{
    public function fields(NovaRequest $request): array
    {
        return [
            ID::make(),
            Text::make(__('fields.name'), 'name'),
        ];
    }
}
  1. Create a custom action to associate a feature to several products.
class AssociateFeatureToProductAction extends Action
{
    public function handle(ActionFields $fields, Collection $models)
    {
       /// associate $fields->get('feature') to products
    }

    public function fields(NovaRequest $request): array
    {
        return [
            BelongsTo::make('Feature', 'feature', FeatureResource::class),
        ];
    }
}
  1. Open a browser and type a search to filter the products list
  2. Select some products, open the action and pick a feature that doesn't match the searched keyword
  3. Execute the action.
  4. The nova::validation.relatable error message will pop.

Workaround:

My quick workaround was to add the following method to FeatureResource:

    public static function buildIndexQuery(NovaRequest $request, $query, $search = null,
        array $filters = [], array $orderings = [],
        $withTrashed = TrashedStatus::DEFAULT): Builder
    {
        // Fixes issue when filtering parent resource while validating a belongsTo field
        if ($request instanceof ActionRequest) {
            $search = null;
        }

        return parent::buildIndexQuery($request, $query, $search, $filters, $orderings, $withTrashed);
    }
@jeremynikolic
Copy link

Thanks for reaching out to us 🙏

We have a fix incoming for this issue 👍

@jeremynikolic
Copy link

jeremynikolic commented Sep 14, 2024

Duplicates #6518

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

No branches or pull requests

2 participants