3

I am having trouble accessing child model repeater field to load without user action, I use RelationController in parent model controller this is what I did in parent controller

class WartaRutin extends Controller
{
    public $implement = ['Backend\Behaviors\ListController',
                         'Backend\Behaviors\FormController',
                         'Backend\Behaviors\RelationController']; 

    public $listConfig = 'config_list.yaml';
    public $formConfig = 'config_form.yaml';
    public $relationConfig = 'config_relation.yaml';      

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

    }

    public function formExtendFieldsBefore($form) {

        if($form->model instanceof Mismaiti\MyWarta\Models\Baptis){

            $iteration8 = $form->fields['peserta']['maxItems'];

            if(is_numeric($iteration8) && $iteration8 > 0) {
            $emptyFields = [];                             
                while($iteration8 > 0) {
                    $emptyFields[] = ['anak' => ' '];                    
                    $iteration8--;
                }
            $form->model->peserta = $emptyFields;
            }

        } 
    } 
}

this child model fields

fields:
    ...        
    peserta:
        label: 'Peserta Baptis'
        ...
        maxItems: '4'
        ...
    form:
        fields:
            anak:
                label: 'Nama Anak'
                ...
            ortu:
                ...
            alamatbaptis:
                ...

parent model fields for this

baptisan:
        label: ''           
        ...
        type: partial
        path: field_baptis
        ...

this is config_relation.yaml

baptisans:
label: Baptisan
view:
    list: $/mismaiti/mywarta/models/baptis/relation_columns.yaml
    toolbarButtons: add|create|remove                  
manage:
    form: $/mismaiti/mywarta/models/baptis/relation_fields.yaml
    list: $/mismaiti/mywarta/models/baptis/relation_columns.yaml 

this relation in parent model that i define

public $belongsToMany = [
     'baptisans' => [
        Baptis::class,'table' => 'mismaiti_mywarta_rutbaptis'
     ]
];

relation in child model

public $belongsToMany = [
    'wartarutin' => [
        WartaRutin::class,'table' => 'mismaiti_mywarta_rutbaptis'            
    ]

];

before i use RelationController, this method which is share by @HardikSatasiya work fine, the repeater field autoload base on defined maxItems without user action. can someone share me how to solve this?

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
Isral Bustami
  • 137
  • 13

1 Answers1

2

You can add this code to your WartaRutin controller to add those empty fields

class WartaRutin extends Controller {

    ...

    public function relationExtendManageWidget($widget, $field, $model)
    {
        // we will do it for this model only
        if ($widget->model instanceof \Mismaiti\MyWarta\Models\Baptis) {
            // fetch max item
            $iteration8 = $widget->fields['peserta']['maxItems'];

            // use loop and add those empty fields
            if(is_numeric($iteration8) && $iteration8 > 0) {

                $emptyFields = [];                             
                while($iteration8 > 0) {
                    $emptyFields[] = ['anak' => ' '];                    
                    $iteration8--;
                }
            $widget->model->peserta = $emptyFields;
        }
    }

    ...

Try this it will surely work for you

if any doubt or if it's not working please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40
  • 1
    it works perfectly.. thank you so much @HardikSatasiya – Isral Bustami Jun 08 '18 at 02:43
  • 1
    for this `$emptyFields[] = ['anak' => ' '];` , can we define value of **anak** dynamic base on the database value of **anak** field.. – Isral Bustami Jun 08 '18 at 02:48
  • 1
    for instance **anak** is a dropdown that has number of options base on record count of **anak** model, excuse my language if you confuse.. – Isral Bustami Jun 08 '18 at 02:55
  • 1
    yes, I am pretty sure we can do this, I need to test it out and will add additional info according to it in answer :). – Hardik Satasiya Jun 08 '18 at 04:51
  • 1
    I have figure it out sir.. I define iteration number by count record of model and define emptyfield with index of anak.. you are the best sir.. thank you – Isral Bustami Jun 08 '18 at 05:02
  • 1
    cool, seem you started to figure out thing on your own :), its great, and thanks for the compliment :) – Hardik Satasiya Jun 08 '18 at 05:03
  • Sir.. with this method that you gave me.. can it also customize width of widget – Isral Bustami Jun 08 '18 at 06:34
  • if you talking about that field options https://octobercms.com/docs/backend/forms#form-field-options then then yes its possible, you can edit its option directly from here `$widget->fields['peserta']['maxItems'] = 10;` like this. if you are talkign about `css` I am not sure :) – Hardik Satasiya Jun 08 '18 at 07:10
  • `$widget->fields['peserta']['span'] = 'auto';` it can make `widget/field 50% in width` and arrage either left side or right side accordingly. – Hardik Satasiya Jun 08 '18 at 07:18