From 4c741125c5a2afd449f644fb1295d81c4302d7fb Mon Sep 17 00:00:00 2001 From: Joris Debonnet Date: Thu, 17 Mar 2016 22:43:26 +0100 Subject: [PATCH] fix #5195: allow empty values in Enum --- model/fieldtypes/Enum.php | 6 +++++- tests/forms/EnumFieldTest.php | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/model/fieldtypes/Enum.php b/model/fieldtypes/Enum.php index cca542aaa..26d9996b1 100644 --- a/model/fieldtypes/Enum.php +++ b/model/fieldtypes/Enum.php @@ -38,7 +38,11 @@ class Enum extends StringField { public function __construct($name = null, $enum = NULL, $default = NULL) { if($enum) { if(!is_array($enum)) { - $enum = preg_split("/\s*,\s*/", trim($enum, ", \t\n\r\0\x0B")); + $enum = preg_split( + "/\s*,\s*/", + // trim commas only if they are on the right with a newline following it + ltrim(preg_replace('/,\s*\n\s*$/','',$enum)) + ); } $this->enum = $enum; diff --git a/tests/forms/EnumFieldTest.php b/tests/forms/EnumFieldTest.php index be1de7754..06531112f 100644 --- a/tests/forms/EnumFieldTest.php +++ b/tests/forms/EnumFieldTest.php @@ -19,6 +19,8 @@ class EnumFieldTest extends SapphireTest { public function testEnumParsing() { $enum = new Enum('testField', " + , + 0, Item1, Item2, Item 3, @@ -29,6 +31,8 @@ class EnumFieldTest extends SapphireTest { "); $this->assertEquals(ArrayLib::valuekey(array( + '', + '0', 'Item1', 'Item2', 'Item 3',