Narek
Narek

Reputation: 3823

Symfony2 notice: Array to string conversion in Sonata Admin

I'm using Sonata admin on Symfony 2.1.3. Now trying to add text input with google map.

Added line in configureFormFields() function:

->add('coordinates', 'contentbundle_coordinates_map', array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

registred in services and created template for this:

{% 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 %}

Can see my field with map in admin content add page, but when I want to submit data getting:

Notice: Array to string conversion in D:\my_vendor_folder\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103

If I change contentbundle_coordinates_map with null:

->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

everything works.

Where is the problem?

UPDATE

Form Type Class:

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';
    }
}

Upvotes: 1

Views: 3096

Answers (1)

RobMasters
RobMasters

Reputation: 4148

You should always define the getParent method for custom Form Types in order to inherit the logic of that particular type. See here for a list of types.

In this case, it looks as though your custom type should be returning text, so add the following to CoordinatesMapType:

public function getParent()
{
    return 'text';
}

As an alternative, if you only need to customise the rendering of the form field then you don't even need to create your own custom form type. See How to customize an Individual field. I think this would only be possible though if you've manually given your form a name. (assuming it's name is "content")

{# 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 %}

In this case, you'd specify the type as null, as in your 2nd example:

->add('coordinates', null, array('required' => false,'attr'=>array('class'=>'mapCoordinate')))

Upvotes: 2

Related Questions