1

I got an error when trying to use

get*field*Options() method 

to

    field: name[field]

I tried to use:

getName[field]Options() method but it return an error.

How can I make this work?

fields.yaml

    temakebum[tema]:                    
            tab: 'Kebaktian Umum & Komisi'             
            label: Tema
            oc.commentPosition: ''
            span: full
            type: text
    temakebum[bacaan]:
            label: 'Bahan Bacaan'
            oc.commentPosition: ''
            span: full
            type: dropdown
            tab: 'Kebaktian Umum & Komisi' 
    temakebum[pujian]:
            label: Pujian
            oc.commentPosition: ''
            span: full
            type: text
            tab: 'Kebaktian Umum & Komisi' 

And in the models

    public function getTemakebum[bacaan]Options() { 
      $bacaan = Db::table('mismaiti_mywarta_jadwlibdh')->where('group','umumraya')->pluck('bacaan','bacaan');
      return $bacaan;
    }

I need to put this several fields in as array into database table.. it is more like the repeater widget.. but the repeater require user to hit add new item button.. i don't want user to hit add new button but i want it there by default

if i use repeater getnamefieldOptions method is work well.. so if i use repeater the method is

getBacaanOptions(){ }

hope i said it clear enough..

LucianDex
  • 469
  • 2
  • 12
Isral Bustami
  • 137
  • 13
  • What is the error ? Why don't you post code samples, example your fields.yaml file and the methods define in your model ? It's hard to help by just guessing – Raja Khoury Feb 07 '18 at 03:42

2 Answers2

1

It is NOT getName[field]Options() instead use get[FieldName]Options()

If your have a model Car and you have a field ( Column ) named Manufacturer then the method name is getManufacturerOptions()

The fields.yaml file of the Car Model should look like this;

  color:
      label: 'Color'  
      type: dropdown

  manufacturer:
      label: 'Manufacturer' 
      type: dropdown

then in Car mode add the method ;

public function getManufacturerOptions() {

    return [
        'volkswagen' => 'Volkswagen',
        'ford'       => 'Ford',
        'toyota'     => 'Toyota'
    ];

  // Or return ManufacturerModel::all()->pluck('name','id');
}

public function getColorOptions() {

    return [
        'black'    => 'Black', 
        'white'    => 'White'
    ];
}

Because the field type is dropdown the method should always return result as an array in the format : Value => Label

If there are no options return an empty array.

When you define the options in the fields.yaml then there's no need to add the method in your model

  color:
      label: 'Color' 
      type: dropdown
      options:
          black: Black
          white: White

UPDATE

1.Add a json column to your DB table $table->json('temakebum')->nullable();

2.Add protected $jsonable = [ 'temakebum '] in your Model Definition

3.Using the naming convention I mentioned above add getBacaanOptions() method to your model

4.Keep your fields.yaml file fields as they are, now the workaround is to change the field type from dropdown to partial for the temakebum[bacaan] field and populate the options there

5.Create a partial in your controller Directory and check the path matches the one in the fields.yaml file

So far fields.yaml looks like this

  temakebum[tema]:
          label: Tema 
          type: text
  temakebum[bacaan]:
          label: 'Bahan Bacaan' 
          type: partial
          path: $/authorName/pluginName/controllers/pluginControllerName/bacaan.htm
  temakebum[pujian]:
          label: Pujian 
          type: text

And your bacaan.htm partial like this :

<?php
$fieldOptions = $model->getBacaanOptions(); // See here we are fetching values
$Temakebum = json_decode($model->attributes['temakebum'], true)  ?: [];
?>
<select class="form-control custom-select" name="YourModelHere[temakebum][bacaan]">
    <?php foreach( $fieldOptions as $key=>$label) { ?>
        <option value="<?= $key ?>" <?php echo ( $Temakebum['bacaan'] == $key) ? "selected" : '';  ?> ><?= $label ?></option>
    <?php } ?>
</select>

( make sure to set the proper select name in the partial YourModelHere[temakebum][bacaan] )

Raja Khoury
  • 3,015
  • 1
  • 20
  • 19
  • My yaml file event[type]: label: Type type: dropdown And in the models public function getEvent[type]Options(){ -- retrieve data from database -- } – Isral Bustami Feb 07 '18 at 05:47
  • i want to make array of 12 fields.. so i just need to save in one field in database – Isral Bustami Feb 07 '18 at 06:34
  • is temakebum the model or column name where you want to store your array? – Raja Khoury Feb 07 '18 at 07:28
  • out of context.. is it possible for repeater field display automatic without user action to hit add new button? – Isral Bustami Feb 07 '18 at 07:45
  • @IsralBustami check new updated answer - Hope it helps. On my end it works fine – Raja Khoury Feb 07 '18 at 08:49
  • Raja i dont know that it can work this way.. so by using partial we can custom fields as how we want it to be.. – Isral Bustami Feb 07 '18 at 10:10
  • @IsralBustami I think the best way to handle arrays is using the repeater widget, it's easier but I chose the partial because in your field.yaml you had field[arrayKey] and the method was throwing an issue, so the workaround is to populate data in the partial to avoid this problem. I am curious to know if there's a better way. – Raja Khoury Feb 08 '18 at 01:14
  • I used repeater but it is require user action to display the repeater.. if there is a way to do autoload for repeater so it automatically display a number set of form.. i would prefer use repeater.. – Isral Bustami Feb 08 '18 at 01:44
1

@Raja Khoury - thanks for making him understand how dropdown works ...

we can use this method for normal fields like manufacturer, but for complex fields we need to use different approach

We need to define this methods in respective model:

First for normal fields there is simple approach get[fieldname]Options

public function get[fieldname]Options($value, $formData)
{
    return ['all' => 'All', ...];
}

Second specific method name approach

fields.yaml

status:
    label: Blog Post Status
    type: dropdown
    options: listBacaan

inside your model

public function listBacaan($fieldName, $value, $formData)
{
    return ['key1' => 'data1', ...];
}

Third General Approach

inside your model

public function getDropdownOptions($fieldName, $value, $formData)
{
    if ($fieldName == 'temakebum[bacaan]') {
        return ['all' => 'All', ...];
    }
    else {
        return ['' => '-- none --'];
    }
}

and for making field as JSON and storing it as an array inside single column @Raja Khoury already answered it in different question you can take reference from there: Octobercms: How can I make a repeater field jsonable because I am creating this repeater field into a different plugin

And if it work for you please up-vote his answer as well :)

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40