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 (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans
v7.12.0@c2fff1e9879494a6f853593b3c517dc9922bbb51
Hello @rUriarteDev
I am not 100% sure but you might be looking for include_all_form_fields and not dependency ?
From my understanding of the code you provided you are trying to get the selected menu_id in some other field of the form and not making chained selects ? set include_all_form_fields => true in your ajax field definition.
Let me know if it solves for you.
Best,
Pedro
Hi @pxpm indeed that work, i think this was a change made in 4.1, so just a lack of documentation, when i did this i follow this, maybe just update the docs there. Thanks
Hi @pxpm indeed that work, i think this was a change made in 4.1, so just a lack of documentation, when i did this i follow this, maybe just update the docs there. Thanks
Hit the same situation, please update the doc pointed by @rUriarteDev, there is no mention to include_all_form_fields, not also in other docs.
Cheers,
Jacq
Sorry guys, just added to the docs. It was an undocumented change indeed, before all form fields were sent by default.
Thanks,
Pedro
Hm... in this case @pxpm ... maybe we should add a new item to the 4.1 Upgrade Guide, to tell people that the default behaviour has changed for include_all_form_fields inside the select2_from_ajax field type? Do I understand this correctly?
Or maybe we can just add it on top of Step 12, something like this??
_Additionally, for the select2_from_ajax field type, the inputs on the main form are no longer sent through the AJAX request by default. If you're using dependencies inside your API response methods, please make sure that you add 'include_all_form_fields' => true to your select2_from_ajax field definition to keep the same behaviour as before._
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
Would it make sense to automatically send the fields listed in 'dependencies' by default? Having to choose between NONE or ALL seems unnecessarily binary.
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.