Object of class Foo\Bar could not be converted to int

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Object of class Foo\Bar could not be converted to int

automatix
This post has NOT been accepted by the mailing list yet.
This post was updated on .
Hello!

I have a form with a MultiCheckBox element protocols.

The structure of the form duplicates the structure of an object of type Foo ('use_as_base_fieldset' => true): every primitive Element relates to a primitive property, every Fieldset relates to a property of a complex type, every Collection relates to an array property with complex types.

The Element protocols (of the fieldset endpoint) relates to the property protocols of the class Endpoint, but this property is an array with objects of type Protocol (not an array with IDs). That means, the object, the form returns, is not complete -- I have to transform the protocol values to Protocol objects before saving it. It's not nice, but it works and I haven't found a more elegant solution. (Btw.: Is there a better solution for this?)

public function init()
{
    parent::init();

    $this->add(
        [
            'type' => 'multi_checkbox',
            'name' => 'protocols',
            'options' => [
                'label' => _('protocols'),
                'label_attributes' => [
                    'class' => 'col-md-1 protocol-field'
                ],
                'value_options' => $this->getValueOptions(),
                'selected' => static::PROTOCOLS_DUMMY_VALUE
            ]
        ]
    );
    ...
}

protected function getValueOptions()
{
    $valueOptions = [];
    foreach (Protocol::PROTOCOLS as $key => $value) {
        $valueOptions[] = [
            'value' => $key,
            'label' => $value
        ];
    }
    $valueOptions[] = [
        ...
    ];
    return $valueOptions;
}

The current issue occurs on binding a saved object to the form (in order to edit an existing entry):

Notice: Object of class My\DataObject\Protocol could not be converted to int in /var/www/path/to/project/vendor/zendframework/zend-form/src/View/Helper/FormMultiCheckbox.php on line 202

My interpretation of this is, that the MultiCheckBox expects an array with values as primitive types (e.g. int). Instead it gets an array with objects (of type Protocol) and tries to use its values for in_array(...) -- and that cannot work.

I don't think it's a very exotic problem. But I could not find any solution.

How to handle such a case?