October: The checkbox value is false instead of true

Created on 24 Oct 2016  路  7Comments  路  Source: octobercms/october

Description

I added a new form field with the $form->addTabFields method. The default value of field was _true_.

Expected behavior

The checkbox or switchbox is checked.

Actual behavior

The checkbox or switchbox is not checked.

Reproduce steps
Event::listen('backend.form.extendFields', function($form)
{
    if ($form->model instanceof Backend\Models\Preference) {
        $form->addTabFields([
            'fp_show_total' => [
                'tab'     => 'Exampe',
                'label'   => 'Something',
                'type'    => 'checkbox',
                'default' => true
            ]
        ]);
    }
}
October build

376

Question

All 7 comments

Ok, I THINK, that's a preferences thing. I mean, if I use that code in any form that is not in the preferences section it shows the default value correctly. But it doesn't with the preferences form (/backend/backend/preferences).

But if I click on "Reset to default" button, it starts working correctly!

I haven't checked the code, but it seems that when you have stored data for preferences and add new fields, they will always be shown as empty even if you give them a default value, because you really don't have that preference with that value, you have an empty value there, so it makes a lot of sense.

As @Patroklo says, it is a quirk with the preferences model. If you are introducing new fields, the values will be empty if the preferences are already established. You should use the "reset to default" to rebuild the preferences from their default values.

@daftspunk I think it may be more closely related to a quirk in the form controller/widget - i.e. default values are not applied in any context other than create (or when the model for the form doesn't exist yet) irregardless of whether there is a value for the field or not.

I was planning on looking into improving that at some point in the future so that default values could apply on dynamic fields that get loaded into a model that doesn't have a value set yet because it wasn't a part of the initial save of the model. Do you have any thoughts on that?

Ok, I trust your judgement on this. I would need to see the problem demonstrated along with the fix and we are good to go.

For sure, I'll take note of this issue and mention it when I make a PR

Hey, i'm having same issue, but in i declared in fields.yaml

        special_offer:
            label: mishai.sitemanager::lang.hotel_model.special_offer
            type: checkbox
            default: true
            tab: mishai.sitemanager::lang.hotel_model.special_offer

looks like default: true simply not working

Might need to log another issue for this @bbrandtlv, please do include replication instructions using the test plugin.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

axomat picture axomat  路  3Comments

mittultechnobrave picture mittultechnobrave  路  3Comments

lukaszbanas-extremecoding picture lukaszbanas-extremecoding  路  3Comments

oppin picture oppin  路  3Comments

dunets picture dunets  路  3Comments