This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Wouter
Wouter

Hi, I try to safe a checkbox list but i get a error.

"Unexpected type of array, should attribute "staatsvorm" be jsonable?" on line 995 of /home/domain/subdomain/test2/vendor/october/rain/src/Database/Model.php

I have in my model:


public function getStaatsvormOptions()
{
    return ['volsrepubliek' => 'Volsrepubliek', 
    'democratie' => 'Democratie', 
    'dictatuur' => 'Dictatuur', 
    'democratie' => 'Democratie', 
    'presidentiele_republiek' => 'Presidentiele republiek', 
    'parlementaire_republiek' => 'Parlementaire republiek', 
    'constitutionele_monarchie' => 'Constitutionele monarchie'];
}

In my controller this list the checkbox:


'staatsvorm' => [ 
                'label'   => 'Staatsvorm',              
                'comment' => 'Welke staatsvorm heeft het land.', 
                'type' => 'checkboxlist',
                'span' => 'left',
                'tab' => 'Algemeen',
            ],

I have realy no idea what i do now wrong why it not can be saved to the db....

Last updated

axomat
axomat

Hi I dont know what the problem is but I notice that the line number cannot be 995 (it is a comment) so I think you should update to latest version then try again. Next idea is to check the yaml (I assume that is what you are showing in the second snippet) because the spacing looks wrong when it is displayed here although that may just be a cut and paste problem.

Wouter
Wouter

There are now update when i try to update. So i think im using the newest version

My config_form.yaml:

# ===================================
#  Form Behavior Config
# ===================================

# Record name
name: LandenInformatie

# Model Form Field configuration
form: $/addrenaline/landeninformatie/models/landeninformatie/fields.yaml

# Model Class name
modelClass: Addrenaline\LandenInformatie\Models\LandenInformatie

# Default redirect location
defaultRedirect: addrenaline/landeninformatie/landeninformatie

# Create page
create:
    title: Create LandenInformatie
    redirect: addrenaline/landeninformatie/landeninformatie/update/:id
    redirectClose: addrenaline/landeninformatie/landeninformatie

# Update page
update:
    title: Edit LandenInformatie
    redirect: addrenaline/landeninformatie/landeninformatie
    redirectClose: addrenaline/landeninformatie/landeninformatie

# Preview page
preview:
    title: Preview LandenInformatie
axomat
axomat

In my controller this list the checkbox:

What do you mean by this in your original post? Do you mean that you have defined the checkboxlist values in your controller? I thing I assumed wrongly you were doing that in the model form yaml file.

Wouter
Wouter

I have in the controller defined the form.

public function formExtendFields($form)
    {
        $form->addTabFields([
'staatsvorm' => [ // lijst staat in plugins/addrenaline/landeninformatie/models/LandenInformatie.php
                'label'   => 'Staatsvorm',              
                'comment' => 'Welke staatsvorm heeft het land.', 
                'type' => 'checkboxlist',
                'span' => 'left',
                'tab' => 'Algemeen',
            ],

Yes u define now the checkbox options in the model. The field is in the controller created. in the model:

    public function getStaatsvormOptions()
{
    return ['volsrepubliek' => 'Volsrepubliek', 
    'democratie' => 'Democratie', 
    'dictatuur' => 'Dictatuur', 
    'democratie' => 'Democratie', 
    'presidentiele_republiek' => 'Presidentiele republiek', 
    'parlementaire_republiek' => 'Parlementaire republiek', 
    'constitutionele_monarchie' => 'Constitutionele monarchie'];
}
 

When I select a checkbox in the form And submit then I get there error on the first post.

Is it About maybe in the db field because it is now varchar field? When I think now do i have to create a relation to save the checkbox valeus ?

Last updated

axomat
axomat

Yes of course, a checkboxlist refers to a relation.

Look at User.php in backend/models and you will see an example of a checkboxlist with a relation of userGroup.

Wouter
Wouter

I see try to create a realtion. But then i get the error:

Class 'landeninformatie\models\landeninformatie' not found

My relation is now:

public $belongsToMany = [
     'staatsvorm' => ['landeninformatie\models\landeninformatie', 'table' => 'addrenaline_landeninformatie_landen_informaties']
    ];    ]

I realy dont onderstand why to create a relation. What the realtion is doing. Wil it put october the valeu's in a separted table and pivot table ?

Is there somewhere a example for this ? I have read many times the pages about realtions but still dont understand what i have to do or do wrong...

Last updated

axomat
axomat

Look at the backend User and UserGroup models. The user groups are chosen with a checkboxlist.

Wouter
Wouter

I have done that but the relation point to Backend\Models\UserGroup. I dont understand where I have to point to. The raltion above is not good. To my model is not right...

To what piont the relation

LaminEvra
LaminEvra

The error you are having is because your model property staatsvorm needs to be added to the property $jsonable of your model like

$jsonable = ['staatsvorm']

Reason is because you are trying to save a list of items which is an array and your model needs to know how to store them in the database(because the database presumably does not natively support array types), so marking the list as jsonable, it( the array) will be converted to json and stored in your atabase as a json string.

When you retrieve it back, the model will automatically convert it to the initial array.

Hope that helps and let me know if you have any questions. Sorry for the long answer. :-)

Edit: reading the comments above, when I had that issue I first thought I needed a relation to solve it(which you could use) but the jsonable property was designed specifically for this scenario. I ended up not using relations.

Last updated

Jan van de Laar
Jan van de Laar

I figured this out yesterday and in the backend all checkboxfields are stored perfectly. But now I've made a frontend view fot the detailsPage and the checkboxed values cannot be shown there because of an error that says: An exception has been thrown during the rendering of a template ("Array to string conversion").

How do I get them values in my frontend? In this case a have several choosen 'coursetypes' that need to be shown on the page.

I tried {{record.cursustype}} but with the above shown error as result. Anyone?

1-11 of 11

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.