On/Off messages not working in Forms

I have the following fields.yaml but the on/off labels for switch is not working

fields:
    section:
        label: 'Kunden Einstellung'
        span: full
        type: section
    status:
        label: ''
        'on': Disable
        'off': Enable
        span: full
        type: switch
    heading:
        label: Heading
        span: full
        type: text
    description:
        label: Description
        size: ''
        span: full
        type: textarea```

Hi,

Looking in the file

modules/backend/widgets/form/partials/_field_switch.htm

You will see that the partial changed from V2 to V3.

<?php
    $trueLabel = $field->config['on'] ?? ($field->options[1] ?? 'backend::lang.form.field_on');
    $falseLabel = $field->config['off'] ?? ($field->options[0] ?? 'backend::lang.form.field_off');
?>

… and the rest that goes with it is not in _field_switch.htm in the V3.

A solution could be to create your own partial using the _field_switch.htm of version V2 in your plugin and using it.

1 Like

Hey,

Thank you for the Kind reply.

My apologies i am very new to V3 and cannot compare it to V3. can you explain it a bit what you means by

… and the rest that goes with it is not in _field_switch.htm in the V3.

A solution could be to create your own partial using the _field_switch.htm of version V2 in your plugin and using it.

i see the label is

<label class="form-check-label" for="<?= $field->getId() ?>">
        <?= e(__($field->label)) ?>
    </label>

Thank you so much.

Hi,

You will see that October is a fantastic tool.

  1. Create a file called ‘_field_switch_v2.htm’, should be located there:

$/yourAuthorName/yourPluginName/controllers/yourControllerName/_field_switch_v2.htm

  1. In the file you just created (_field_switch_v2.htm), add this code:
<?php
    $previewMode = false;
    if ($this->previewMode || $field->readOnly) {
        $previewMode = true;
    }
?>
<!-- Switch -->
<?php
    $trueLabel = $field->config['on'] ?? ($field->options[1] ?? 'backend::lang.form.field_on');
    $falseLabel = $field->config['off'] ?? ($field->options[0] ?? 'backend::lang.form.field_off');
?>
<div class="field-switch">
    <label><?= e(trans($field->label)) ?></label>
    <?php if ($field->comment): ?>
        <p class="help-block"><?= $field->commentHtml ? trans($field->comment) : e(trans($field->comment)) ?></p>
    <?php endif ?>
</div>

<input
    type="hidden"
    name="<?= $field->getName() ?>"
    value="0"
    <?= $previewMode ? 'disabled="disabled"' : '' ?> />

<label class="custom-switch" <?= $previewMode ? 'onclick="return false"' : '' ?>>
    <input
        type="checkbox"
        id="<?= $field->getId() ?>"
        name="<?= $field->getName() ?>"
        value="1"
        <?= $previewMode ? 'readonly="readonly"' : '' ?>
        <?= $field->value == 1 ? 'checked="checked"' : '' ?>
        <?= $field->getAttributes() ?>>
    <span><span><?= e(trans($trueLabel)) ?></span><span><?= e(trans($falseLabel)) ?></span></span>
    <a class="slide-button"></a>
</label>

  1. Update your fields.yaml :
fields:
    section:
        label: 'Kunden Einstellung'
        span: full
        type: section
    status:
        label: ''
        span: full
        type: partial
        path: $/yourAuthorName/yourPluginName/controllers/yourControllerName/_field_switch_v2.htm
        'on': Disable
        'off': Enable
    heading:
        label: Heading
        span: full
        type: text
    description:
        label: Description
        size: ''
        span: full
        type: textarea```
2 Likes

Thank you for detailed tutorial. i already implemented it and it is amazing.

October <3