When submitting a form and validations are not passed, the form is returned but the data on all the select2 multiple are not appearing.


This is for select2_from_array
This is probably some type of bug and I think it may already have an issue (but I can't see which one yet).
Are there any errors in the JavaScript console (or laravel logs)?
@lloy0076 No error at all in the console. This is also appearing for select_from_array multiple.
@MickaelTH - I wonder if data from the database in select2's is working _at all_...
Hey @lloy0076 just using this field now and can see an issue. It saves to the DB, but then on refresh it doesnt correctly load all entries, only the first. So a second save while wipe the DB of all data. Ill see what I can find now.
@lloy0076 Everything is well saved in the DB it appears in the UPDATE view. the issue is when validations rules don't get passed during submit of the form. Surely something wrong with the old() data?

For me, this seems to only affect select2_from_ajax_multiple
The problem is in /vendor/backpack/crud/src/resources/views/fields/select2_from_ajax_multiple.blade.php.
You should be able to edit resources/views//vendor/backpack/crud/src/resources/views/fields/select2_from_ajax_multiple.blade.php to fix.
Probably a more elegant solution, but I think something like this does the trick:
@php
$connected_entity = new $field['model'];
$connected_entity_key_name = $connected_entity->getKeyName();
$old_value = old($field['name']) ? old($field['name']) : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : false ));
@endphp
to
<!-- select2 from ajax multiple -->
@php
$connected_entity = new $field['model'];
$old = null;
if (old($field['name'])) {
$old = $connected_entity::whereIn('id',old($field['name']))->get();
}
$connected_entity_key_name = $connected_entity->getKeyName();
$old_value = $old ? $old : (isset($field['value']) ? $field['value'] : (isset($field['default']) ? $field['default'] : false ));
@endphp
Hi @blocher I am not able to test your idea because this is appearing for me with 'select2_from_array' and the option 'allows_multiple' to true.
@blocher I think I've just fixes your bug in select2_from_ajax_multiple in https://github.com/Laravel-Backpack/CRUD/pull/1018
@MickaelTH I haven't been able to get the same thing. Can you please copy-paste your addField statement?
@tabacitu Sure here is the code:
$this->crud->addField(
[ // select_from_array
'name' => 'modules',
'label' => "3. Select your modules",
'type' => 'select2_from_array',
'options' => [
'All' => 'All',
'Financials and Procurement' => 'Financials and Procurement',
'Financials Procurement and SCM' => 'Financials Procurement and SCM',
'Financials Procurement Manufacturing and SCM' => 'Financials Procurement Manufacturing and SCM',
'Financials' => 'Financials',
'Manufacturing' => 'Manufacturing',
'Manufacturing and SCM' => 'Manufacturing and SCM',
'Procurement' => 'Procurement',
'Supply Chain Management' => 'Supply Chain Management'
],
'allows_null' => false,
'allows_multiple' => false, // OPTIONAL; needs you to cast this to array in your model;
'tab' => 'General Settings',
'wrapperAttributes' => ['class' => 'form-group col-md-6'],
],
'both'
);
@tabacitu, I've tested this in the backpack demo and can confirm it's happening with at least the select_from_array and select2_from_array fields.
I'll do more testing to see if there are any other fields affected by this. Like @MickaelTH said, it has probably something to do with the old() input.
As soon as I'm done, I'm gonna get started on a PR.
@tabacitu, done testing and I've found that, besides the select_from_array and select2_from_array, in case of a validation failure, the checkbox and base64_image field types also don't fill in the old input. Oh, and also the password field, but that's the correct behavior :p.
I'm gonna try to fix all of these in a single PR.
Well, this is odd.
So, apparently the select fields don't work as expected when there are values in the database already.
Example:
select_multiple, select_from_array, select2_multiple or select2_from_array field@tabacitu, I would expect the old submit data to be shown in this case as well, right?
Thank you @tumf87 ! Merged your PR, so it's now fixed on dev. It'll be available publicly by the end of the week! Cheers!
@tabacitu @tumf87 On create in case of a validation failure, image field types also don't fill in the old input.

@wtime I'm afraid that's an HTML spec issue. After refresh the browser won't give you access to the filesystem, so you can "reselect" the image. It would be a security risk.
Most helpful comment
Well, this is odd.
So, apparently the select fields don't work as expected when there are values in the database already.
Example:
select_multiple,select_from_array,select2_multipleorselect2_from_arrayfield@tabacitu, I would expect the old submit data to be shown in this case as well, right?