Skip to content

ListboxField discarding valid values based on type of first valid values #11956

@jmlagace

Description

@jmlagace

Module version(s) affected

6.x, >= 5.1

Description

In version 5.0 merely used the list of valid values to determine what would be saved to the database.

Starting with version 5.1 it guesses the type of the first value of the valid values and further selects the values to be based based on the type of that first value.

I happened to have a list of valid values where the first value was an integer. As a result, string values would no longer save.

How to reproduce

Code

<?php

namespace App\Model;

use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\ListboxField;

class TestObject extends DataObject
{
    private static $table_name = 'TestObject';

    private static $db = [
        'Values' => 'Text',
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        // This order will cause the value ABC123 to be selectable but will be filtered out when saving because the first value is a numeric string, which causes the ListboxField to filter values as numeric strings.
        $source = [
            '123'    => 'Numeric string',
            'ABC123' => 'Alpha-numeric string',
        ];

        // Case A: numeric-string first (this is the broken case)
        $fields->push(
            ListboxField::create(
                'Values',
                'Values (numeric string first)',
                $source
            )
        );

        return $fields;
    }
}

Steps to recreate

  1. Add this DataObject to your project with an Admin.
  2. In the admin, select both values and save.
  3. Only the numerical value is saved.

NOTE: If the values have a string value first all the valid values will be saved.

Possible Solution

I would revert back to the behaviour in 5.0.

I tried to find the reason for the change of behaviour with no success. The change as introduced in commit e1d10a0 with the comment: "NEW ListboxField react field schema"

Additional Context

No response

Validations

  • Check that there isn't already an issue that reports the same bug
  • Double check that your reproduction steps work in a fresh installation of silverstripe/installer (with any code examples you've provided)

PRs

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions