Search code examples
phpsymfonyeasyadmin

Easy admin refuses entity update unless I edit image field


I am fixing an existing Symfony 5.4 website running on PHP 8.0.18. The back office is handled by EasyAdmin 3.4.

I can't figure out what's wrong. Like the title says, when I go to edit an "Event" entity, the save buttons won't work at all unless I re-upload a different event picture. No amount of editing the other fields will work, and I can use the save buttons on other entities even if I have made no modification to the entity. I've looked though my configuration and entity setup but so far, I don't get it.

Edit: other entities with ImageField also refuse to be updated unless I've re-uploaded something. I found a temporaty fix by using setRequired(false) in the event crud conf, but an image is definitely required in this case, so I'm just setting myself up for a different kind of failure if I'm not mistaken. Is this really the only way?

Event entity:

<?php

namespace App\Entity;

use App\Repository\EventRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=EventRepository::class)
 */
class Event
{
    // ...

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $src;

    // ...

    public function getSrc(): ?string
    {
        return $this->src;
    }

    public function setSrc(string $src): self
    {
        $this->src = $src;

        return $this;
    }

    // ...
}

Event crud controller:

<?php

namespace App\Controller\Admin;

use App\Entity\Event;
use App\Entity\TranslationString;
use App\Entity\TranslationText;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;

class EventCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Event::class;
    }

    public function configureCrud(Crud $crud): Crud
    {
        return $crud
            ->setSearchFields([
                'date',
                'end',
                'title.fr',
                'title.en',
                'body.fr',
                'body.en',
                'alt.fr',
                'alt.en',
            ])
            ->setDefaultSort(['archived' => 'ASC','date' => 'DESC',]);
    }

    public function configureFields(string $pageName): iterable
    {
        return [
            DateField::new('date'),
            DateField::new('end'),
            TextField::new('titleFr'),
            TextField::new('titleEn')->hideOnIndex(),
            BooleanField::new('isShow'),
            BooleanField::new('archived'),
            TextareaField::new('bodyFr'),
            TextareaField::new('bodyEn')->hideOnIndex(),
            ImageField::new('src')
                ->setBasePath('img/events')
                ->setUploadDir('www/img/events'),
            TextareaField::new('altFr')->hideOnIndex(),
            TextareaField::new('altEn')->hideOnIndex(),
        ];
    }
    
    public function createEntity(string $Fqcn): Event
    {
        return (new Event)
            ->setAlt(new TranslationText)
            ->setTitle(new TranslationString)
            ->setBody(new TranslationText);
    }
}


Solution

  • I had the same problem and I think that the following code will help

    public function configureFields(string $pageName): iterable {
        $imageField = ImageField::new('image', 'Image')->setUploadDir('public/uploads/images/')->setBasePath('uploads/images/');
        if ($pageName != 'new') {
            $imageField->setRequired(false);
        }
        
        return [
            TextField::new('title'),
            $imageField,
            TextEditorField::new('description')->setNumOfRows(20),
            UrlField::new('ticketOfficeLink'),
            AssociationField::new('eventStates')
        ];
    }