[英]Symfony2 notice: Array to string conversion in Sonata Admin
我在Symfony 2.1.3上使用Sonata admin 。 現在嘗試使用谷歌地圖添加文本輸入。
在configureFormFields()
函數中添加了一行:
->add('coordinates', 'contentbundle_coordinates_map', array('required' => false,'attr'=>array('class'=>'mapCoordinate')))
在服務中注冊並為此創建模板:
{% block contentbundle_coordinates_map_widget %}
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="{{ asset('js/add.scripts.js') }}"></script>
<input type="text" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
<div id="add_map" class="map" style="width:500px;height:300px;"></div>
{% endblock %}
可以在管理內容添加頁面中使用地圖查看我的字段,但是當我想提交數據時:
Notice: Array to string conversion in D:\my_vendor_folder\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103
如果我將contentbundle_coordinates_map更改為null :
->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))
一切正常。
問題出在哪兒?
更新
表單類型類:
namespace Map\ContentBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CoordinatesMapType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'contentbundle_coordinates_map';
}
}
您應該始終為自定義表單類型定義getParent
方法,以便繼承該特定類型的邏輯。 有關類型列表,請參見此處。
在這種情況下,看起來您的自定義類型應該返回文本,因此將以下內容添加到CoordinatesMapType
:
public function getParent()
{
return 'text';
}
作為替代方案,如果您只需要自定義表單字段的呈現,那么您甚至不需要創建自己的自定義表單類型。 請參閱如何自定義個人字段。 我認為這只有在您手動為表單命名時才有可能。 (假設它的名字是“內容”)
{# This is in the view where you're rendering the form #}
{% form_theme form _self %}
{% block _content_coordinates_widget %}
<div class="text_widget">
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>
<script type="text/javascript" src="{{ asset('js/add.scripts.js') }}"></script>
<input type="text" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
<div id="add_map" class="map" style="width:500px;height:300px;"></div>
</div>
{% endblock %}
在這種情況下,您可以將類型指定為null
,如第二個示例所示:
->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.