我想编辑默认表单模板,以便在显示上传字段时包含图像的缩略图预览,因此我在我的实体中放入一个名为getFormThumbnail()的方法,返回路径。当实体具有它可行的方法时,但是当它没有时,我得到一个错误:
An exception has been thrown during the rendering of a template ("Warning: call_user_func_array() expects parameter 1 to be a valid callback, class 'Acme\UserBundle\Entity\UserTranslation' does not have a method 'getFormThumbnail' in C:\...\symfony\vendor\knplabs\doctrine-behaviors\src\Knp\DoctrineBehaviors\Model\Translatable\TranslatableMethods.php line 140") in kernel.root_dir/Resources/views/Form/form_div_layout.html.twig at line 43.
500 Internal Server Error - Twig_Error_Runtime
我正在编辑{% block form_widget_simple %}
我补充说:
{% if type == 'file' %}
{% if form.vars.form.parent.vars.value.getFormThumbnail is not null %}
<img src="{{ form.vars.form.parent.vars.value.getFormThumbnail | imagine_filter('thumb_250') }}" style="display: block; margin-bottom: 10px">
{% endif %}
{% endif %}
当getFormThumbnail存在时,此代码可以正常工作。所以我尝试添加is defined
并且它不起作用,我甚至尝试过这样的事情:
form is defined
and
form is not null
and
form.vars is defined
and
form.vars is not null
and
form.vars.form is defined
and
form.vars.form is not null
and
form.vars.form.parent is defined
and
form.vars.form.parent is not null
and
form.vars.form.parent.vars is defined
and
form.vars.form.parent.vars is not null
and
form.vars.form.parent.vars.value is defined
and
form.vars.form.parent.vars.value is not null
and
form.vars.form.parent.vars.value.getFormThumbnail is defined
and
form.vars.form.parent.vars.value.getFormThumbnail is not null
仍然,我总是得到相同的错误,顺便说一句,错误中的行是我测试的行,如果定义了getFormThumbnail
因此,如果错误为does not have a method
,是否有办法测试它是否有方法?
答案 0 :(得分:0)
您只能为该字段创建自定义片段。因此,您无需检查方法是否存在http://symfony.com/doc/current/cookbook/form/form_customization.html#how-to-customize-an-individual-field。
或者您可以创建表单类型扩展名:http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html