code200.miha
code200.miha

How to set default values for repeater field in settings?

Thanks!

Mohsin
Mohsin

In your form field yaml file under repeater use the default attribute as follows

characters:
    label: Characters
    type: repeater
    default:
        0:
            name: Jim Moriarty
            phone: 1234554321
        1:
            name: Irene Adler
            is_enabled: 1234554321
    form:
        fields:
            name:
              label: Name

            phone:
              label: Phone

This example is in the case where the fields are just name and phone. In your case, you will need to write the code based on your fields. This is basically an Array in the YAML language and it gets converted to json when it stores in the database. If you use complex configs like groups in the repeater config then the group meta too must be passed in this array. Best idea is to make a repeater without the default, add all the values you need as default. Now open your database and in the repeater field get the json, convert that to the YAML array format and paste in under default. That will work best.

Last updated

Troiscent
Troiscent

When I try your syntax, I got the following error :

substr() expects parameter 1 to be string, array given" on line 469 of 
/vendor/laravel/framework/src/Illuminate/Support/Str.php

Here is my syntax for that field :

    forms:
        label: Forms
        type: repeater
        span: full
        tab: Infos
        form:
            fields:
                url:
                    label: Url
                    span: auto
                lifetime: 
                    label: Lifetime
                    span: auto
                description:
                    label: Description
                    span: full
        default:
            0:
                url: /contact
                lifetime: "3 mois apr├Ęs l'envoi"
                description: "Demande de renseignement par l'internaute"
LukeTowers
LukeTowers

That should be fixed as of Build 447, see https://github.com/octobercms/october/pull/4062.

1-4 of 4