ImageField required when updating/edit
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);

@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!
I have the same issue.
@javiereguiluz can you fix this problem quickly pls ? :pray:
A solution could be
$picture = ImageField::new('picture')
->setUploadDir('public/uploads/gifts')
->setBasePath('uploads/gifts')
->setSortable(false)
->setFormTypeOption('required' ,false)
Nowdays there is a method so you can use it for ex. like this:
ImageField::new('picture')->setRequired($pageName !== Crud::PAGE_EDIT);
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] : [])