Search code examples
symfonyeasyadmin

submit form with a virtual property


I'm currently working with the 2.3 version of the easy-admin bundle in Symfony 4.

I try to create a virtual property for the new view. I have the following configuration:

easy-admin:
    entities:
        FieldTemplate:
            class: App\Entity\FieldTemplate                  
            edit: 
                fields:
                    - { property: imageToFill, type_options: { block_prefix: 'field_to_fill'} }

according to https://stackoverflow.com/a/58710631/6734243 and the documentation, I need to add setter and getter to the virtual entity which I did :

//src/Entity/FieldTemplate.php

/**
 * @ORM\Entity(repositoryClass="App\Repository\FieldTemplateRepository")
 */
class FieldTemplate
{
    //virtual functions to display the image of the fieldTemplate in easyadminbundle

    public function getImageToFill()
    {
        return $this->getImage();
    }

    public function setImageToFill()
    {
        //do nothing
        return $this;
    }

The field is displayed properly but when I save I get the following error, which suggest that there is no setter for my virtual property.

Could not determine access type for property "imageToFill" in class "App\Entity\FieldTemplate": Neither the property "imageToFill" nor one of the methods "addImageToFill()"/"removeImageToFill()", "setImageToFill()", "imageToFill()", "__set()" or "__call()" exist and have public access in class "App\Entity\FieldTemplate".

Is this behaviour a bug or do I misunderstand something ?


Solution

  • Not an easy one. When the form is handled, it send something to the setImageToFill() property.

    even if the imageToFill property is a virtual property and doesn't send anything to the db

    So even if it does nothing, add a dummy argument to your setter such as

    setImageToFill($dummy){
        // do nothing 
        return $this
    }
    

    and it will work like a charm