Search code examples
phpsymfonysymfony2-easyadmin

Symfony 2.8 Easyadmin DataTransformer for enum(yes, no) fields to represent boolean in forms


I use easyadmin to generate backend needs.I really loved to control boolean values with nice switch off / on ui feature in the list view.

But I don't use boolean values as tinyint(1) 1 or 0, I use as ENUM('yes', 'no'). So in symfony forms I use DataTransformer to convert yes to 1 when sending to view, and 1 to yes when sending to model. But I couldn't find any related documentation how could I use data transformer or event listener in easyadmin. I see several dispatches and events are exists in easyadmin.

Events

Do you have any idea how could I add dataTransformer preferably, or event listener.

Any help would be appreciated.


Solution

  • I find an another solution which solves the problem without any need of DataTransformers or Listeners. In Doc it mentions about virtual Entity methods. So I put following methods which haven't related with any entity properties directly.

    public function setBoolHost($isHost)
    {
        return $this->setHost($isHost ? EntityInterface::YES : EntityInterface::NO);
    }
    
    public function getBoolHost()
    {
        return ($this->getHost() == EntityInterface::YES) ? true : false;
    }
    

    So in config.yml easy_admin: config looks as following

    entities:
        Members:
            list:
                fields:
                    - { property: 'boolHost', type: 'toggle', label : 'Is Host ?'}
    

    I hope it is help to someone who needs a solution to kind of this problem.