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

[Bug] Image Validation when more than one in the same CRUD [pro] #5706

Closed
manfield opened this issue Oct 24, 2024 · 5 comments
Closed

[Bug] Image Validation when more than one in the same CRUD [pro] #5706

manfield opened this issue Oct 24, 2024 · 5 comments
Labels
question Further information is requested

Comments

@manfield
Copy link

manfield commented Oct 24, 2024

Bug report

What I did

Let's have a CRUD controller with these fields:

            [
                'name'  => 'cover',
                'type'  => 'image',
                'label' => trans('custom.cover'),
                'withMedia' => ['displayConversions' => 'thumbnail'],
                'validationRules' => 'base64mimes:jpg,jpeg,gif,png',
                'hint'       => trans('custom.image_upload_rule'),
            ],
            [
                'name'  => 'cover2',
                'type'  => 'image',
                'label' => trans('custom.cover'),
                'withMedia' => ['displayConversions' => 'thumbnail'],
                'validationRules' => 'base64mimes:jpg,jpeg,gif,png',
                'hint'       => trans('custom.image_upload_rule'),
            ],

What I expected to happen

Let's add the two images at the same time on a new object:

Screen.Recording.2024-10-24.at.22.40.24.mp4.mp4

All good!

Let's modify the second now:

Screen.Recording.2024-10-24.at.22.43.10.mp4.mp4

As you can see the first of the two fields raises a validation error, though:

  • it should not be involved in validation, since I modified the second one only
  • the two images uploaded don't break rule constraints (both are jpg)

What happened

Image validation has some bugs when using more than one image field on the same CRUD controller

Note: the same error is reported even if the image is in a repeatable/relationship field's subfield

Backpack, Laravel, PHP, DB version

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

### PHP VERSION:
8.3.9

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, ast, bcmath, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, igbinary, intl, exif, msgpack, mysqli, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, redis, shmop, SimpleXML, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, memcached, Zend OPcache, xdebug

### LARAVEL VERSION:
11.23.5.0

### BACKPACK PACKAGE VERSIONS:
backpack/basset: 1.3.6
backpack/crud: 6.7.41
backpack/devtools: 3.1.6
backpack/generators: v4.0.6
backpack/language-switcher: 2.0.0
backpack/medialibrary-uploaders: 1.2.1
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.21
backpack/settings: 3.1.1
backpack/theme-tabler: 1.2.13
backpack/translation-manager: 1.0.4
@manfield
Copy link
Author

manfield commented Oct 25, 2024

Note: same issue if I use Validation rule in the Http\ModelRequest class:

The following example is for a similar use case, not the one reported in the issue. But actually it behaves the same.

    public function rules()
    {
        return [
            'title' => 'required|string',
            'cover' => 'base64image|base64mimes:jpg,png,jpeg,png|base64max:2048',
            'gallery.*.gallery_image' => 'base64image|base64mimes:jpg,png,jpeg,png|base64max:2048',
            'content_blocks.*.image' => 'base64image|base64mimes:jpg,png,jpeg,png|base64max:2048',
        ];
    }

@karandatwani92
Copy link
Contributor

Hey @pxpm

I think the problem is our Image field is unnecessarily sending the stored path("string") when no file is selected, causing the validation error. where if filled it - it expects base64.

  "image" => "uploads/monsters/image_field/d9cccdddb451b47866acff0f9db5746d.jpg"

@karandatwani92 karandatwani92 removed the v7 label Oct 28, 2024
@manfield
Copy link
Author

manfield commented Nov 5, 2024

Hi! I wonder if this issue could be solved soon or a workaround could be provided.

@karandatwani92
Copy link
Contributor

karandatwani92 commented Feb 4, 2025

Hey @manfield

Sorry, it took so long to get back to this.

At first, I thought it was a bug, but when I inspected the image field again. I don't find it to be a BUG. Here is how the image field works:

  • It sends a base64 string when submitting a new image.
  • It sends an existing image path when there are no updates on the old image.
  • It sends an empty '' string when no image is attached or when you want to make it empty/remove.

I tried your case, and it worked for me with the following validation rules:

public function rules()
    {
        $rules = [
            'cover' => 'required|base64image',
            'cover2' => 'required|base64image',
        ];

        if ($this->isMethod('put')) {
            $rules['cover'] = 'required';
            $rules['cover2'] = 'required';
        }

        return $rules;
    }
Screen.Recording.2025-02-04.at.2.13.10.PM.mov

Just try it and let me know your thoughts. Sorry again, It took so long.

@manfield
Copy link
Author

manfield commented Feb 4, 2025

Hi @karandatwani92 ,
thanks for your feedback.

It works for me.
I wonder if it should be added to the documentation.

Thanks a lot again

@manfield manfield closed this as completed Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants