EasyAdminBundle icon indicating copy to clipboard operation
EasyAdminBundle copied to clipboard

ImageField required when updating/edit

Open devcut opened this issue 5 years ago • 5 comments

Hello,

When i edit my entity with already image in database, the imageField show the required HTML5 tooltip...

$picture = ImageField::new('picture')
      ->setUploadDir('public/uploads/gifts')
      ->setBasePath('uploads/gifts')
      ->setSortable(false);

Capture d’écran 2020-12-01 à 09 30 56

devcut avatar Dec 01 '20 08:12 devcut

@devcut I came up with a bit of a dirty fix. You can override the ea form theme (or specific parts of it). Check out the docs for more info https://symfony.com/doc/current/bundles/EasyAdminBundle/fields.html#design-options I've added a little

<script>
        var fileInput = document.querySelector('#{{ form.file.vars.id }}');
        var labelText = fileInput.nextElementSibling.innerText;
        if( '' !== labelText ) {
            fileInput.removeAttribute('required');
        }
</script>

This checks if the label has any text in it. If so that means there's an image so the input's required attribute is removed. Remember to set allow_delete to false on the fields form type options.

Would be great to see a solid built in solution for this issue!

sietseveenman avatar Dec 10 '20 10:12 sietseveenman

I have the same issue.

Huluti avatar Dec 10 '20 18:12 Huluti

@javiereguiluz can you fix this problem quickly pls ? :pray:

devcut avatar Dec 11 '20 07:12 devcut

A solution could be

$picture = ImageField::new('picture')
      ->setUploadDir('public/uploads/gifts')
      ->setBasePath('uploads/gifts')
      ->setSortable(false)
      ->setFormTypeOption('required' ,false)

Joancesar avatar Jan 13 '21 18:01 Joancesar

Nowdays there is a method so you can use it for ex. like this:

ImageField::new('picture')->setRequired($pageName !== Crud::PAGE_EDIT);

kwolniak avatar Sep 21 '22 13:09 kwolniak

If the image is not nullable, better to do:

ImageField::new('picture')
    ->setRequired($pageName !== Crud::PAGE_EDIT)
    ->setFormTypeOptions($pageName == Crud::PAGE_EDIT ? ['allow_delete' => false] : [])

ytilotti avatar Feb 28 '23 14:02 ytilotti