Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single select field empty value validation with integer values #11502

Closed
2 tasks done
mfendeksilverstripe opened this issue Dec 9, 2024 · 3 comments
Closed
2 tasks done
Assignees

Comments

@mfendeksilverstripe
Copy link
Contributor

mfendeksilverstripe commented Dec 9, 2024

Module version(s) affected

5.3.3

Description

Form field validation enhancements that were introduced in CMS 5.3 are causing unexpected issues in some specific cases. I'll showcase this on a DropDownField but it's possible that other fields that extend SingleSelectField are also impacted.

If a DropDownField is used with integer values and the field allows empty default (allows a value to not be provided) the form validation will not accept the empty value.

How to reproduce

My code sample uses a content block but I don't think this issues is related to Elemental, I just need to have the inline editing context.

  • set up a test page with a test block
  • the block needs an Int type DB field
  • create a DropDownField for the DB field and provide some test values
  • use the inline edit form to save changes on the block such as updating title (keep the default empty value on the test field as is)

Expected outcome

The changes get saved without validation message as empty value is valid (we don't have any validation set up)/

Actual outcome

Field validation message pops up and prevents the changes to be saved.

Screenshot 2024-12-09 at 2 46 04 PM

Code snippets

private static array $db = [
    'TestField' => 'Int',
];

public function getCMSFields()
{
    $fields = parent::getCMSFields();
    $values = [
        1 => 'First value',
        2 => 'Second value',
    ];

    $fields->addFieldsToTab('Root.Main', [
        $testField = DropdownField::create('TestField', 'Just a test', $values)
    ]);
    $testField->setEmptyString('-- select --');

    return $fields;
}

Possible Solution

The issue comes from the validation code in SingleSelectField::validate(). The empty values comes in as '0' (string) which is not considered an empty value. It's likely that casting this value as an integer would solve the issue.

Tried several other possible values (passed via the $values) but neither one worked:

  • 0
  • -1
  • ''

Additional Context

We had similar issues with RequiredFields validator where we ended up changing the validation comparison method:

// Disallow empty values of any kind (also cater to JSON encoded values)
$value = is_string($value)
    ? json_decode($value, true) ?? $value
    : $value;

Not sure if this is related though. I'm just illustrating that the condition to determine that value is empty needs a refinement.

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

@GuySartorelli
Copy link
Member

Form field validation enhancements that were introduced in CMS 5.3 are causing unexpected issues

What specific changes in CMS 5.3 are causing this? It sounds like you've got some additional context that isn't included here. There's no change to the validate method on SingleSelectField between 5.2 and 5.3, though that seems to be the method you've indicated as being problematic.

It's also interesting that this isn't reproducible without being inside an inline-editable elemental block. That suggests to me that the bug is more likely to have been introduced by a change in elemental than by a change in framework.

@GuySartorelli
Copy link
Member

Having played around with this a bit, I can confirm the following:

  1. The problem is specifically with the SingleSelectField react component. Its initial prop value is "0". If you select a value, then select the empty value, its value becomes an empty string, which is the correct value. The validation logic in PHP is correct, it's the initial prop value which is incorrect.
  2. This is only surfacing in this scenario in CMS 5.3 because inline-editable blocks weren't validated prior to that.

@emteknetnz
Copy link
Member

Linked PR has been merged, it will be automatically tagged shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants