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

program.es
program.es

I had tried following.

  • Make custom form widget and loop onAddItem function 5 times.
  • Add data-request="formMyJson::onAddItem;formMyJson::onAddItem;"
  • Create another dropdown with 1, 5 and 10 value. Set Repeater depensOn to it and try to set minItems option with filterFields.
  • Create trigger in dropdown and set minItems. Not work at all. Please advice.
daftspunky
daftspunky

You could just set the attribute to an empty array with 5 items in it ready to go

program.es
program.es

Thank you for your reply, @daftspunk.

May you explain more detail? Where I should set attribute?

daftspunky
daftspunky

Take a look at the documentation on accessors:

https://octobercms.com/docs/database/mutators#accessors-and-mutators

public function getMyRepeaterAttribute($value)
{
    // New records only
    if (!$this->exists) {
        return [... array with 5 items already in it ...];
    }
}
program.es
program.es

Please find following code. I want to click on balloon-selector and set repeater quantity. I used accessors for set and return JSON value. But after refresh with depensOn function. Repeater quantity still the same.

fields.yaml

    stamps_json:
        dependsOn: stamp_items
        minItems: 30
        maxItems: 100
        span: full
        type: repeater
        style: collapsed
        form:
            fields:
                active_icon:
                    label: 'Stamp Icon'
                    type: mediafinder
                    mode: image
                    default: /default/stamped.png
                inactive_icon:
                    label: 'Unstamp Icon'
                    type: mediafinder
                    mode: image
                    default: /default/default.png
                point:
                    label: Point
                    span: left
                    default: 100
                    required: true
                    type: text
    stamp_items:
        label: 'Stamp Items'
        options:
            10: '10 items'
            20: '20 items'
            30: '30 items'
            40: '40 items'
            50: '50 items'
            60: '60 items'
            70: '70 items'
            80: '80 items'
            90: '90 items'
            100: '100 items'
        showSearch: false
        span: left
        default: '10'
        required: true
        type: balloon-selector

MyModel.php

public function getStampsJsonAttribute($json)
{

    $jsonArray = json_decode($json,true);
    $countJsonArray = @count($jsonArray);

    $Form = post('Form');

    if(isset($Form['stamp_items']) && $countJsonArray<$Form['stamp_items'])
    {
        $maxStampIndex = max(array_keys($jsonArray));
        $maxStampItem = $jsonArray[$maxStampIndex];

        for ($i=$maxStampIndex; $i <($Form['stamp_items']-1) ; $i++) {
            array_push($jsonArray, $maxStampItem);
        }
        return json_encode($jsonArray);

    } else if(isset($Form['stamp_items']) && $countJsonArray>$Form['stamp_items'])
    {
        $jsonArray = array_slice( $jsonArray, 0, $Form['stamp_items']);
        return json_encode($jsonArray);

    } else
    {
        return $json;
    }
}

public function getStampItemsAttribute($value)
{
    //Set selected
    return count($this->stamps_json);
}

1-5 of 5

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