Silverstripe-framework: Enum without default is selecting a default in DropdownField that has an empty default

Created on 9 Nov 2017  路  6Comments  路  Source: silverstripe/silverstripe-framework

I have

    private static $db = [
        'FooterSection' => 'Enum("Navigate, About, Legal")'
    ];

and

        $fields->addFieldsToTab('Root.Main', [
            DropdownField::create('FooterSection', 'Footer section', [
                'Navigate' => 'Navigate',
                'About'    => 'About',
                'Legal'    => 'Legal'
            ])->setEmptyString('Not displayed in footer')
        ], 'Metadata');

When i create a new page, "Navigate" is always selected, instead of "Not displayed in footer"

Adding:

    private static $defaults = [
        'FooterSection' => 0
    ];

Fixes it for me

PRs

affectv4 changpatch efformedium impacmedium typbug

Most helpful comment

private static $db = [
    'FooterSection' => 'Enum("Navigate, About, Legal", null)'
];

All 6 comments

SilverStripe version?

4

@zanderwar after this fix, you can use null as a second arg in your $db spec, and it'll correctly set an empty default.

private static $db = [
    'FooterSection' => 'Enum("Navigate, About, Legal", null)'
];
Was this page helpful?
0 / 5 - 0 ratings

Related issues

chillu picture chillu  路  3Comments

maxime-rainville picture maxime-rainville  路  3Comments

Cheddam picture Cheddam  路  3Comments

ntd picture ntd  路  4Comments

ScopeyNZ picture ScopeyNZ  路  5Comments