I was just creating some nodes programmatically and was running into an error that the value I was setting for my list field wasn't an allowed option.

It turns out, when I had input my allowed values, I had used the following form:

key | value

So when I was trying to set the field value to 'key', the system was expecting 'key '.

While this isn't a bug, this might be an issue that others would run into too. I don't think that trimming the values would cause problems for anyone, and would possibly avert trouble for the small number of people who put spaces on either side of the separator.

Comments

dddave’s picture

Version: 7.0 » 8.x-dev

Kicking upwards...

Anonymous’s picture

Issue tags: +Novice

This should be a simple fix. Adding Novice tag.

kars-t’s picture

Status: Active » Needs review
StatusFileSize
new715 bytes

Adding some trims is really easy. But I am not sure if the description of the textarea should be altered as well and if we need updated tests for this?

And maybe we should only trim the key but not the value. Doesn't makes much sense to me to use spaces in the label but we would probably change an expected behavior?

eloivaque’s picture

I try the patch of #3 comment, and its working for me.

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Makes sense to me, patch is fine enough.

xjm’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs tests

Looks great!

We should add some simple automated test coverage for this fix, and perhaps an inline comment explaining why we're trimming.

plopesc’s picture

StatusFileSize
new1.9 KB

Hello

Patch re-rolled including basic test and inline comment explaining why we're trimming.

Regards

yesct’s picture

This could also use a tests only patch (to show the bot will fail without the fix, that the fix works and the tests will work to show the fail).

plopesc’s picture

Of course, re-rolling only test and combined patches.

Regards.

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Fine for me.

catch’s picture

Version: 8.x-dev » 7.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Maybe we need an actual widget for this. Until that point this looks good.

Committed/pushed to 8.x, might be backportable?

plopesc’s picture

Hello

Attaching basic backport patch, including tests.

However, I suppose that there will be problems for people who actually uses values that includes spaces.

One option could be use hook_update_N to trim all stored values in the DB.

What do you think?

dcam’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Needs tests

Setting status...

Status: Needs review » Needs work

The last submitted patch, trim_allowed_options-1029060-12-tests.patch, failed testing.

star-szr’s picture

Status: Needs work » Needs review

@plopesc - If you upload the test-only patch first, then the combined patch (as you did in #9), the system comment and setting to "needs work" (as in #14) won't happen. Provided the combined patch passes testing, of course :)

parthipanramesh’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

The latest patch does work fine for me. In my opinion it's a good solution to use trim. Thanks!

David_Rothstein’s picture

Title: Should allowed options for list fields trim() the strings? » (Upgrade/migration path) Should allowed options for list fields trim() the strings?
Version: 7.x-dev » 8.x-dev
Priority: Minor » Major
Status: Reviewed & tested by the community » Needs work
Issue tags: -Novice +D8 upgrade path

I tested this patch on a D7 site with a field that already had spaces at the end of some of the list options. Regardless of whether the site admin put the spaces there intentionally or not, this causes big problems if the site already has data in the field. If you go to the Field UI settings page for the field and try to save it for any reason, you get a validation error - "Allowed values list: some values are being removed while currently in use" - and then you're kind of stuck.

I believe this would affect a site that upgraded to Drupal 8 too, and therefore probably needs some kind of upgrade/migration path to fix.

Even then, I'm not sure we can fix it in Drupal 7. People use machine names in code all the time (in addition to the database), so switching the machine names from underneath them sounds like a bad idea in a stable release.

penyaskito’s picture

Do we need to massage the data in D6-D7 => D8 migration then?

penyaskito’s picture

\Drupal\migrate_drupal\Plugin\migrate\process\d6\FieldSettings::getSettings already trims the allowed values setting. I cannot find where we do the same with saved field values (if we do at all).

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • catch committed 5ff1cee on 8.3.x
    Issue #1029060 by plopesc, Kars-T: Should allowed options for list...

  • catch committed 5ff1cee on 8.3.x
    Issue #1029060 by plopesc, Kars-T: Should allowed options for list...

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

  • catch committed 5ff1cee on 8.4.x
    Issue #1029060 by plopesc, Kars-T: Should allowed options for list...

  • catch committed 5ff1cee on 8.4.x
    Issue #1029060 by plopesc, Kars-T: Should allowed options for list...

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

quietone’s picture

Version: 8.9.x-dev » 8.0.x-dev
Status: Needs work » Fixed

This issue was committed to 8.0 and was reopened for backport to Drupal 7. According to the testing in #17 the patch causes problems in Drupal 7 and recommends that it not be applied to Drupal 7. The remaining question was should field values be trimmed when migrating. From a migrate perspective changing the source value is not what the use expects and there needs to be strong evidence that it is needed. The Migrate API has been in use for a while now and I don't recall an issue to trim field values. If someone thinks that should be done then open an issue in the 'migration system' component.

Changing the status to Fixed and changing the version to when the commit was made.

Thanks all!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.