Symfony form: customize the setter that is called

倖福魔咒の 提交于 2019-12-01 05:38:21

You can define your foo field in the form as not mapped and then add listener on the POST_SUBMIT that will call your doSomething() method:

$builder->add('foo', null, array('mapped' => false))
    ;

    $builder->addEventListener(
        FormEvents::POST_SUBMIT,
        function(FormEvent $event) {
            $entity = $event->getForm()->getData();
            $entity->doSomething($event->getForm()->get('foo')->getData(), true);
        }
    );

It will not call $entity->setFoo($value). Instead it will call $entity->doSomething($value, true) as you wished.

Check this post about DataMapper (it starts from explaining Value Objects, but link will scroll down to Data Mappers header directly). It's very useful but missed in Symfony docs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!