From cb37e5fb8726a0fa5ef37f5cb2f654475e9e4fb6 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Thu, 16 Dec 2021 11:52:11 +0000 Subject: [PATCH] FIX: Ensure extra classes are copied over to readonly/disabled fields --- src/Forms/FormField.php | 3 ++- tests/php/Forms/FormFieldTest.php | 8 ++++++++ tests/php/Forms/TreeMultiselectFieldTest.php | 4 ++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Forms/FormField.php b/src/Forms/FormField.php index 2e2caab9b..ecd9ede9a 100644 --- a/src/Forms/FormField.php +++ b/src/Forms/FormField.php @@ -1355,13 +1355,14 @@ class FormField extends RequestHandler $field = $classOrCopy::create($this->name); } + $extraClasses = $this->extraClasses ? array_values($this->extraClasses) : []; $field ->setValue($this->value) ->setForm($this->form) ->setTitle($this->Title()) ->setLeftTitle($this->LeftTitle()) ->setRightTitle($this->RightTitle()) - ->addExtraClass($this->extraClass) // Don't use extraClass(), since this merges calculated values + ->addExtraClass(implode(' ', $extraClasses)) // Don't use extraClass(), since this merges calculated values ->setDescription($this->getDescription()); // Only include built-in attributes, ignore anything set through getAttributes(). diff --git a/tests/php/Forms/FormFieldTest.php b/tests/php/Forms/FormFieldTest.php index 411d81e1d..63fb2e3f6 100644 --- a/tests/php/Forms/FormFieldTest.php +++ b/tests/php/Forms/FormFieldTest.php @@ -259,6 +259,14 @@ class FormFieldTest extends SapphireTest $this->assertStringNotContainsString('readonly="readonly"', $field->getAttributesHTML()); } + public function testReadonlyPreservesExtraClass() + { + $field = new FormField('MyField'); + $field->addExtraClass('myextraclass1')->addExtraClass('myextraclass2'); + $field->setReadonly(true); + $this->assertStringContainsString('myextraclass1 myextraclass2', $field->getAttributesHTML()); + } + public function testDisabled() { $field = new FormField('MyField'); diff --git a/tests/php/Forms/TreeMultiselectFieldTest.php b/tests/php/Forms/TreeMultiselectFieldTest.php index e1f5354a2..a43a2b6d1 100644 --- a/tests/php/Forms/TreeMultiselectFieldTest.php +++ b/tests/php/Forms/TreeMultiselectFieldTest.php @@ -203,7 +203,7 @@ class TreeMultiselectFieldTest extends SapphireTest $this->assertSame('TreeDropdownField', $schemaDataDefaults['component']); $this->assertSame(sprintf('%s_Holder', $field->ID()), $schemaDataDefaults['holderId']); $this->assertSame('Test tree', $schemaDataDefaults['title']); - $this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']); + $this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']); $this->assertSame('field/TestTree/tree', $schemaDataDefaults['data']['urlTree']); $this->assertSame(true, $schemaDataDefaults['data']['showSearch']); $this->assertSame('(Search or choose File)', $schemaDataDefaults['data']['emptyString']); @@ -239,7 +239,7 @@ class TreeMultiselectFieldTest extends SapphireTest $this->assertSame('TreeDropdownField', $schemaDataDefaults['component']); $this->assertSame(sprintf('%s_Holder', $field->ID()), $schemaDataDefaults['holderId']); $this->assertSame('Test tree', $schemaDataDefaults['title']); - $this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']); + $this->assertSame('treemultiselectfield_readonly multiple searchable', $schemaDataDefaults['extraClass']); $this->assertSame('field/TestTree/tree', $schemaDataDefaults['data']['urlTree']); $this->assertSame(true, $schemaDataDefaults['data']['showSearch']); $this->assertSame('(Search or choose File)', $schemaDataDefaults['data']['emptyString']);