8

I'm trying to use a lightswitch-field in a plugin-config. But for some reason it won't get saved. The lightswitch-field is always off.

protected function defineSettings()
{
    return array(
        ...
        'mailerShowInfo' => array(AttributeType::Bool, 'default' => false),
        ...
    );
}

_settings.html:

{{  forms.lightswitchField({
        label: "Show info in sidebar?"|t,
        id: 'mailerShowInfo',
        name: 'mailerShowInfo',
        value: settings.mailerShowInfo,
        required: true
    })
}}

I tried to use AttributeType::Number instead, and even used settype() in prepSettings() to force a boolean conversion, but still nothing.

Any idea what I'm missing?

1 Answer 1

13

I believe you need to set the on value of your lightswitch field.

on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""

So, the full settings of your twig code would be:

{{  forms.lightswitchField({
        label: "Show info in sidebar?"|t,
        id: 'mailerShowInfo',
        name: 'mailerShowInfo',
        value: settings.mailerShowInfo,
        required: true
        on: (settings.mailerShowInfo is defined) ? settings.mailerShowInfo : ""
    })
}}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.