简体   繁体   English

管理类中的Sonata自定义字段

[英]Sonata custom field in admin class

I created a new field type and I am trying to use it in sonata. 我创建了一个新的字段类型,并试图在奏鸣曲中使用它。 In one of my admin classes I have the following: 在我的一个管理课程中,我有以下内容:

$formMapper
                    ->add('name', 'text')
                    ->add('img_home', new \MyBundle\Form\Type\ImageType(), array(
                        'required' => false,
                        'mapped' => false,
                        'label' => 'home set image',
                        'attr' => array('src' => '/' . $home)
                            )
                    )

When I do this on the img_home field I trigger this exception 当我在img_home字段上执行此操作时,将触发此异常

'Please define a type for field ...' “请为字段定义类型...”

https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/master/Builder/FormContractor.php#L56 https://github.com/sonata-project/SonataDoctrineORMAdminBundle/blob/master/Builder/FormContractor.php#L56

If I comment out this exception everything works fine. 如果我注释掉此异常,一切正常。 I see the field I want without any issues. 我看到了我想要的字段,没有任何问题。

How can I fix this without having to change the code? 如何解决此问题而不必更改代码?

Found the solution. 找到了解决方案。 After reading the code I noticed that there is a 4th argument you can pass and sonatas code checks if the array passed has they 'type' key. 阅读代码后,我注意到可以传递第4个参数,奏鸣曲代码检查传递的数组是否具有“类型”键。 So I just added : 所以我刚刚补充说:

array('type' => 'string')

As the 4th parameter 作为第四个参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM