question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] select2 field that depends on another field not working after 4.1 upgrade

See original GitHub issue

Bug report

After upgrading to 4.1 the select2 from ajax fields depending on other fields stop working, do some debug and notice that when making the ajax call the request is arriving empty to my controller, so i cant do the filter. Im using the old method that calls to the custom controller to respond to the ajax call. The debuger shows the error when trying to look in $form[‘menu_id’] (Undefined index: menu_id) The index function to manage the ajax call is

 public function index(Request $request)
    {
        $search_term = $request->input('q');

        $form = collect($request->input('form'))->pluck('value', 'name');

        $options = collect();

        // if no category has been selected, show no options
        if (!$form['menu_id']) {
            return [];
        }

        // if a category has been selected, only show articles in that category
        if ($form['menu_id']) {
            $options = $this->platosDisponibles($request);
        }

        if ($search_term) {
            $options=$options->filter(function ($item) use ($search_term) {
                // replace stristr with your choice of matching function
                return false !== stristr($item->descripcion, $search_term);
            })->paginate(10);

        } else {
            $options = $options->paginate(10);
        }

        return $options;
    }

My field definition

        $this->crud->addField(
            [  // Select2
                'label' => "Plato",
                'type' => "select2_from_ajax",
                'name' => 'plato_id', // the db column for the foreign key
                'entity' => 'plato', // the method that defines the relationship in your Model
                'attribute' => "descripcion", // foreign key attribute that is shown to user
                'model' => "App\Models\Plato", // foreign key model
                'data_source' => url("admin/calculoPreparacionPlatos"), // url to controller search function (with /{id} should return model)
                'placeholder' => 'Seleccione un plato', // placeholder for the select
                'minimum_input_length' => 0, // minimum characters to type before querying results
                'dependencies' => ['fecha', 'menu_id'],
                //'method' => 'get',
            ]
        );

My route inside custom.php

Route::group([
    'prefix'     => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['web', config('backpack.base.middleware_key', 'admin')],
    'namespace'  => 'App\Http\Controllers\Admin',
], function () {
Route::get('/calculoPreparacionPlatos', 'Extra\CalculoPreparacionPlatos@index');
}

PHP 7.2.11 (cli) (built: Oct 10 2018 02:04:07) ( ZTS MSVC15 (Visual C++ 2017) x64 ) Copyright © 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright © 1998-2018 Zend Technologies with Xdebug v2.7.2, Copyright © 2002-2019, by Derick Rethans

LARAVEL VERSION:

v7.12.0@c2fff1e9879494a6f853593b3c517dc9922bbb51

BACKPACK VERSION:

4.1.6@cbd4143d3eb8302916012af205565cd3183f274f

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
tabacitucommented, Jul 24, 2020

Hmm… that’s… wow… yeah… that sounds way better, doesn’t it @pxpm 😆 ? If you have dependencies, you MUST need them in the AJAX call.

I’ve opened #3066 for this, made a summary so it’s easier to understand what we’re all taking about, instead of having to read a bunch of threads. Let’s move the conversation there, should be easier to follow.

1reaction
pxpmcommented, Jul 21, 2020

Indeed @tabacitu . That was introduced here: https://github.com/Laravel-Backpack/CRUD/pull/2757/commits/2a0d1215a2d9c2a34882a644a01b2f45654f3159

I’v just submited the PR to the docs.

Best, Pedro

Read more comments on GitHub >

github_iconTop Results From Across the Web

Common problems - The jQuery replacement for select boxes
Select2 does not function properly when I use it inside a Bootstrap modal. This issue occurs because Bootstrap modals tend to steal focus...
Read more >
Select2 doesn't work when embedded in a bootstrap modal
According to the official select2 documentation this issue occurs because Bootstrap modals tend to steal focus from other elements outside of the modal....
Read more >
Changelog — CKAN 2.9.7 documentation
Broken link in “Upgrading CKAN's dependencies” doc page (#3637); Default logo image not properly saved (#3656); Activity test relies on datetime.now() (#3644) ...
Read more >
Changelog - Admin Columns Pro
Fixed an issue where an error occurred when using the Custom Field column with the inline editing setting set to 'false'. Only one...
Read more >
BackpackForLaravel/Lobby - Gitter
I wanted to add more fields to the register form but laravel says it doesnt ... I am trying to update it to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found