0

can i set the repeater widget to auto load when the form open?

it has max item property, can it auto load to max item number

so user dont need to hit the prompt button

re-updated

this my fields.yaml

fields:
nowarta:
    label: 'No. Warta'
    oc.commentPosition: ''
    span: auto
    placeholder: 'format penulisan ''No. 34 Tahun XIX'''
    type: text
tanggal:
    label: Tanggal
    oc.commentPosition: ''
    mode: date
    format: 'd - F - Y'
    span: right
    type: datepicker
    ignoreTimezone: false
renungan:
    label: 'Renungan Mingguan'
    oc.commentPosition: ''
    span: full
    type: section
renungan[judul]:
    label: 'Judul Renungan'
    oc.commentPosition: ''
    span: storm
    type: text
    cssClass: 'col-sm-6 col-sm-push-0'
renungan[bahanbaca]:
    label: 'Bahan Bacaan'
    oc.commentPosition: ''
    span: storm
    type: text
    cssClass: col-sm-6
renungan[isi]:
    label: Renungan
    oc.commentPosition: ''
    span: storm
    cssClass: 'col-sm-12 col-sm-push-0'
    type: richeditor
    size: huge
tabs:
fields:
    temakebum:
        label: 'Kebaktian Umum'
        oc.commentPosition: ''
        span: full
        type: section
        tab: 'Kebaktian Umum & Komisi'
    temakebum[tema]:
        tab: 'Kebaktian Umum & Komisi'
        label: Tema
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-11
        type: text
    temakebum[bacaan]:
        label: 'Bahan Bacaan'
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-6
        type: text
        tab: 'Kebaktian Umum & Komisi'
    temakebum[pujian]:
        label: Pujian
        oc.commentPosition: ''
        span: storm
        cssClass: col-sm-6
        type: text
        tab: 'Kebaktian Umum & Komisi'
    kebum:
        label: ''
        oc.commentPosition: ''
        prompt: 'Tambah Data'
        maxItems: '3'
        span: full
        type: repeater
        tab: 'Kebaktian Umum & Komisi'
        form:
            fields:
                jeniskeb:
                    label: 'Jenis Kebaktian'
                    oc.commentPosition: ''
                    span: storm
                    cssClass: col-sm-4
                    type: dropdown
                    options: jenisKeb
                khotbah:
                    label: Pengkhotbah
                    oc.commentPosition: ''
                    span: storm
                    cssClass: col-sm-4
                    placeholder: ''
                    type: text
              

dd($form->fields)

array:6 [▼
  "nowarta" => array:5 [▶]
  "tanggal" => array:7 [▶]
  "renungan" => array:4 [▶]
  "renungan[judul]" => array:5 [▶]
  "renungan[bahanbaca]" => array:5 [▶]
  "renungan[isi]" => array:6 [▶]
]

and this what i have done in controller as you want it to be..

class WartaRutin extends Controller
{
    public $implement = ['Backend\Behaviors\ListController','Backend\Behaviors\FormController','Backend\Behaviors\ReorderController'    ];
    
    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $reorderConfig = 'config_reorder.yaml';

    public function __construct()
    {
        parent::__construct();
        BackendMenu::setContext('Mismaiti.MyWarta', 'main-menu-item', 'side-menu-rutin');
    }

    public function formExtendFieldsBefore($form) {    
    // we are checking we are creating record or updating
    // or even we can use $form->context here but I used $model->id
    // if $form->context == 'update'
    // if $form->context == 'create'
        if(is_null($form->model->id)) {
            // create time
            $iteration = $form->fields['kebum']['maxItems'];

            if(is_numeric($iteration) && $iteration > 0) {
                $emptyFields = [];
                while($iteration > 0) {                        
                    $emptyFields[] = ['jeniskeb' => ''];
                    $iteration--;
                }
                $form->model->kebum = $emptyFields;
            }

        }
        
    }
}

and it return an error when i tried to execute-- i have replace all repeater_data to kebum

"Undefined index: kebum" on line 30 of C:\xampp\htdocs\mywarta\plugins\mismaiti\mywarta\controllers\WartaRutin.php

am i missing something here..?

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
Isral Bustami
  • 137
  • 13
  • share your full `fields.yaml` as error is on line `$iteration = $form->fields['kebum']['maxItems'];` so print `dd($form->fields);` that and make sure its there, and `$form->model->repeater_data` need to replace it – Hardik Satasiya Feb 10 '18 at 08:25
  • @HardikSatasiya i have re updated my question.. with my fields.yaml included.. – Isral Bustami Feb 10 '18 at 08:47
  • `dd($form->fields)` doesnt display kebum field.. why? – Isral Bustami Feb 10 '18 at 08:59
  • ok its inside tab so it will be no there it will be inside `tabs`, so just you need to replace `$form->fields['kebum']['maxItems']` to `$form->tabs['fields']['kebum']['maxItems']`, let me know if it works or not – Hardik Satasiya Feb 10 '18 at 09:11
  • Yayy.. it works.. thank you very.. very much.. – Isral Bustami Feb 10 '18 at 09:26
  • If i want to implement to other repeater field.. i just need another iteration variable and make else if condition. Am i right? – Isral Bustami Feb 10 '18 at 09:28
  • i guess `no need of else` just use `if condition logic` and repeat it with `other field` inside `if condition` [updated answer repeat * * logic * * portion] – Hardik Satasiya Feb 10 '18 at 09:41
  • Yes.. it is only need another if condition.. i tried and it also work.. you're the man.. up vote 4 you.. – Isral Bustami Feb 10 '18 at 09:47
  • @HardikSatasiya one more thing inside the repeater field i have dropdown field with 3 values.. is it possibe to auto selected those values in each repeater item.. – Isral Bustami Feb 10 '18 at 10:06
  • yes where you set `$emptyFields[] = ['jeniskeb' => ''];` for each field it will be repeated, so some how each time you need to set ``$emptyFields[] = ['jeniskeb' => 10];`` or some relevant id so for `first time 10, second time 20 etc ...` , and `this 10 ...20 will be `id` of that `dropdown's` selected value`, so when it will be rendered it will be pre-selected first 10 then 20 etc ... – Hardik Satasiya Feb 10 '18 at 10:11
  • I see.. the thing is i retrieve those value from database using pluck function.. i call name and name on both parameter inside pluck function.. – Isral Bustami Feb 10 '18 at 10:24
  • So if we define `id` inside `$emptyFields[]` we need to define `id++` also or not? – Isral Bustami Feb 10 '18 at 10:44
  • for your drop-down if your values are like `1=>some item1 , 2 => some item2` then you can specify id = 2 to show selected value "some item2" you can not use `id++` because it will be `sequential 1 . 2 . 3` so how do it will match with drop-down ids ? you need to put them as per your requirement as i showed .. if i put 2 then it will show "some item2" selected so its up to you how you set it – Hardik Satasiya Feb 10 '18 at 12:15
  • @HardikSatasiya my dropdown value is ` item1 => item1, item2=>item2 ` that is how i retrieve from database using pluck function.. – Isral Bustami Feb 10 '18 at 12:21
  • yes so just set `$emptyFields[] = ['jeniskeb' => 'item1'];` to show `item1 ` selected and if you set `$emptyFields[] = ['jeniskeb' => 'item2'];` it will show `item2` selected – Hardik Satasiya Feb 10 '18 at 12:26
  • It is selected item1 at this moment for repeater form 1 till form 3.. can it selected item2 for repeater form2 and item3 for repeater form3.. – Isral Bustami Feb 10 '18 at 12:59
  • if its so then declare `$id = 1;` outside of `while loop` then use `$emptyFields[] = ['jeniskeb' => 'item' . $id++];` it set `item1` for first field `item2` for second field and `item3` for third field. – Hardik Satasiya Feb 10 '18 at 13:13
  • @HardikSatasiya Yess.. it works.. and for next dropdown value is `item one => item one,item two => item two, etc` i made an array `$item = ['one','two','three','four']` and `$id = 0` so i put `$emptyFields[] = ['jeniskebkom' => 'item' . $item[$id++]]` but it doesnt work.. am i missing something? – Isral Bustami Feb 10 '18 at 23:49
  • it check its `value vs key` so if your are using `pluk` so if you use `pluk('name', 'id'')` then array will generate like `[ id_here => name_here ]` so for the `['jeniskebkom' => 'xxx']` here xxx must be one of the `id_here` not the `name_here` then it will work – Hardik Satasiya Feb 11 '18 at 06:38
  • I tried using `pluck('name','id')` it display name on dropdown but when you save it.. in database store id not name.. i dont know how to retrieve it and turn it to name on the frontend.. – Isral Bustami Feb 11 '18 at 07:43
  • It works now.. just need space after item before closing quote.. `'item '.$item[$id++]` now i am stuck to define repeater inside repeater.. – Isral Bustami Feb 11 '18 at 07:48
  • please don't make it so much complex, try to think another way and rearrange your field and make it simple , if you make it more complex at last you are the one who has to mange that so. – Hardik Satasiya Feb 11 '18 at 07:52
  • 1
    Yes.. you are right.. i should make simple.. again and again thank you so very much for being so helpfull.. i still a lot of thing in my mind.. i will make another question for that.. – Isral Bustami Feb 11 '18 at 07:56
  • @HardikSatasiya pls.. i am curious how to define repeater inside repeater for `$iteration` is it possible? – Isral Bustami Feb 11 '18 at 11:59
  • 1
    @HardikSatasiya i have found the solution.. just need to specify emptyfield array.. thanks hardik.. – Isral Bustami Feb 12 '18 at 04:52

1 Answers1

3

hmm, we can use one trick, that as you may only need this at create time or may be you can extend it at update time as well,

I used field repeater_data you can replace it with your field field

fields.yaml

fields:

    ... other fields ...

    repeater_data:
        label: Repeater
        oc.commentPosition: ''
        prompt: 'Add new item'
        maxItems: '5'
        span: auto
        type: repeater
        form:
            fields:
                title:
                    label: Text
                    oc.commentPosition: ''
                    span: auto
                    type: text
                category:
                    label: Dropdown
                    oc.commentPosition: ''
                    options:
                        1: 'Item 1'
                        2: 'Item 2'
                    span: auto
                    type: dropdown

controller

inside your controller you need to add this method and code

public function formExtendFieldsBefore($form) {

    // we are checking we are creating record or updating
    // or even we can use $form->context here but I used $model->id
    // if $form->context == 'update'
    // if $form->context == 'create'
    if(is_null($form->model->id)) {

        // create time **logic**
        $iteration = $form->fields['repeater_data']['maxItems'];
        if(is_numeric($iteration) && $iteration > 0) {
            $emptyFields = [];
            while($iteration > 0) {

                // here you need to provide at least one field 
                // which you used in repeater I used `title`
                // and let it be empty or may be some default value 
                $emptyFields[] = ['title' => ''];
                $iteration--;
            }
            // dummy fields assignment to current form model
            $form->model->repeater_data = $emptyFields;
        }
        // **logic**

    }
    else {

        // if you need to adjust data in update time
        // you need to find difference and then need to add
        // additional dummy data as we are adding above
    }
}

There is 2 scenarios create time and update time

Create time

When user is creating record we will add dummy fields so repeater will show them as they are already there and it is done using maxItems property of that field so its fully dynamic, now user don't need to press that button.

Update time ( Suppose maxItems=5)

Now for 2nd scenario we have else condition if you wish you can add some logic and do your stuff there, Suppose user may add only 2 records at a time so next time we need to add 3 dummy fields (2 filled + 3 dummy = total 5 as maxItem) so you can calculate there and add it from else part.

I hope this will help you, If you find any difficulties please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • i tried and it return an error.. i have updated my question include what i had done.. tell me what have i done wrong? – Isral Bustami Feb 10 '18 at 07:54
  • 2
    ok field is inside tab so it will be no there it will be inside tabs, so just you need to replace `$form->fields['kebum']['maxItems']` to `$form->tabs['fields']['kebum']['maxItems']`, let me know if it works or not – Hardik Satasiya Feb 10 '18 at 09:13