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

Image field in repeatable #2709

Closed
maig81 opened this issue Apr 25, 2020 · 2 comments
Closed

Image field in repeatable #2709

maig81 opened this issue Apr 25, 2020 · 2 comments
Labels

Comments

@maig81
Copy link
Contributor

maig81 commented Apr 25, 2020

Bug report

What I did

Added "image" field in repeatable

What I expected to happen

Show the preview of the image after I made the save function in my model. My model json_decodes the repeatable fields, iterates through them, and saves the images if any are uploaded. Then it changes the image data to the URL and saves the JSON to the database.

What happened

There was no image preview, as the JS does not fill the "src" attribute of the field.

What I've already tried to fix it

I have added JS in "newRepeatableElement()" function in the repeatable.blade.php:

function newRepeatableElement(container, field_group, values) {
            var new_field_group = field_group.clone();

            new_field_group.find('.delete-element').click(function(){
                $(this).parent().remove();
            });

            if (values != null) {
                new_field_group.find('input, select, textarea').each(function () {
                    if ($(this).data('repeatable-input-name')) {
                        $(this).val(values[$(this).data('repeatable-input-name')]);
                    }
                });
                
                // NEW PART
                new_field_group.find('img').each(function () {
                    $(this).attr('src', values['image']);
                });

            }

            container.append(new_field_group);
            initializeFieldsWithJavascript(container);
        }

I am sure this is not best practice, so I`m not committing it under a pull request :)

Backpack, Laravel, PHP, DB version

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

PHP VERSION:

PHP 7.3.1 (cli) (built: Jan 9 2019 22:20:52) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.1, Copyright (c) 1998-2018 Zend Technologies

LARAVEL VERSION:

v6.18.10@9177744ccdd8d5db970fdff2383fe89c2e94aabe

BACKPACK VERSION:

4.1.x-dev@78a89604e4d8571671bb9ec6f2d626a1e8150078

@tabacitu
Copy link
Member

@maig81 thanks a lot for the bug report! Indeed - it was a known bug, that image didn't work inside repeatable fields, and one of the reasons 4.1 was still in beta 😄

I think you'll be glad to know I've just pushed a fix for this - 4.1 now has both image and base64_image working inside repeatable. Please run composer update backpack/crud to get the latest, and let me know if you encounter any issues - you shouldn't 😄

Cheers!

@maig81
Copy link
Contributor Author

maig81 commented Apr 29, 2020

Great :) Glad you solved it so fast 👍
I will try it out and let you know if I find something fishy :D

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants