Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[Bug] Doesn't fully disable a field #914

Closed rahman-nero closed 6 months ago

rahman-nero commented 6 months ago

Bug report

What I did

I have a list of fields that has to be disabled. I pass them to js and run crud.field('field').disable(), but it doesn't work properly. I have to mention, it works perfectly fine for usual fields, but not for multiselect fields.

So, I have a field that called tags with multiselect attribute. When I disable the field, It disables but in the end it will appear in request as tags => null

What I expected to happen

I expected the field wouldn't be sent

What I've already tried to fix it

I fixed it by simple cleaning formData before submitting the form. Here is code:

function disableFieldsFromForm(listDisabledFields) {

    if (typeof listDisabledFields === 'undefined') {
        return;
    }

    listDisabledFields.forEach((item) => {
        crud.field(item).disable();
    });

    const form = document.querySelector("form");

    form.addEventListener('formdata', (e) => {
        const formData = e.formData;

        listDisabledFields.forEach((item) => {

            if (item.includes('[]')) {
                item = item.replace('[]', '');
            }

            formData.delete(item);
        });
    });
}

More details

CrudController:

$this->crud->addFields([
            [
                'tab'     => trans($this->trans_prefix . 'tabs.' . 'appeal'),
                'label'   => trans($this->trans_prefix . 'relationship.' . 'tags'),
                'type'    => 'relationship',
                'name'    => 'tags',
                'wrapper' => [
                    'class' => 'form-group col-lg-4 col-md-6 col-sm-12',
                ],
                'options' => (function ($query) {
                    return $query->where('model_type', Appeal::class)->get();
                }),
            ],
]);

Model.php, relationshiop:

    public function tags()
    {
        return $this->morphToMany(Tag::class, 'model', EntityTag::class);
    }

JS code that I run:

crud.field('tags[]').disable();

I expect that tags field will not be in request, but got this:

  #parameters: array:28 [▼
      "_token" => "9pGK4mZ3IqWYybO5Zix3eYT1M0vA5Qsb7gtolY5U"
      "_method" => "PUT"
      "_http_referrer" => "http://localhost:8000/appeal/appeal/1/edit"
      "id" => "1"
      "tags" => null
      ....
]

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 8.1.27 (cli) (built: Dec 21 2023 13:36:46) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.27, Copyright (c) Zend Technologies

### LARAVEL VERSION:
10.35.0.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.2.2
backpack/crud: 6.4.2
backpack/devtools: 2.0.3
backpack/generators: v4.0.2
backpack/logmanager: v5.0.1
backpack/permissionmanager: 7.1.1
backpack/pro: 2.0.20
backpack/revise-operation: 2.0.0
backpack/settings: 3.1.0
backpack/theme-coreuiv2: 1.2.2
backpack/theme-coreuiv4: 1.1.1
rahman-nero commented 6 months ago

While writing this issue, I found more elegant way to fix that....

We just have to call disable(), two times:

crud.field('tags').disable();
crud.field('tags[]').disable();

First call will delete from formData (the field won't be sent in request). Second call will disable from HTML view (it will disable field so that it can't be used).

Well I hope u fix it in future. Make it just one call

pxpm commented 6 months ago

Thanks for your report @rahman-nero

Indeed some fields require special treatment to properly "disable", "enable" etc. For example: https://github.com/Laravel-Backpack/CRUD/blob/87be4529a445513b04ba8256eec978e94c50e244/src/resources/views/crud/fields/summernote.blade.php#L60

I will investigate this further and get back with the results.

Cheers

pxpm commented 6 months ago

Thanks again @rahman-nero

I've just released backpack/pro 2.1.12 with the fix in this field and similar select2 multiples that had the same issue. 👍

It should take a little bit for our private repository to pick the changes, and you should then be able do a composer update and get the fix.

Let me know if you are still experiencing the issue after the upgrade.

Remember to clear the cache, and make sure you are not overwriting the files in your resources/ folder. 🙏

Cheers