From 9ade8131a3d89035c872cb987d1df6fa15cdd8d7 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 2 May 2024 12:02:28 +1200 Subject: [PATCH] FIX Handle getting HasOneRelationFieldInterface passed as an array --- src/Forms/RequiredFields.php | 7 +++++- src/Forms/SearchableDropdownField.php | 3 ++- tests/php/Forms/RequiredFieldsTest.php | 33 +++++++++++++++++--------- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/Forms/RequiredFields.php b/src/Forms/RequiredFields.php index 918a32a1121..3f0e055c0b7 100644 --- a/src/Forms/RequiredFields.php +++ b/src/Forms/RequiredFields.php @@ -113,7 +113,12 @@ public function php($data) if ($formField instanceof FileField && isset($value['error']) && $value['error']) { $error = true; } else { - $error = (count($value ?? [])) ? false : true; + if (is_a($formField, HasOneRelationFieldInterface::class) && isset($value['value'])) { + $stringValue = (string) $value['value']; + $error = in_array($stringValue, ['0', '']); + } else { + $error = (count($value ?? [])) ? false : true; + } } } else { $stringValue = (string) $value; diff --git a/src/Forms/SearchableDropdownField.php b/src/Forms/SearchableDropdownField.php index 901a88739ec..6d01feb8237 100644 --- a/src/Forms/SearchableDropdownField.php +++ b/src/Forms/SearchableDropdownField.php @@ -5,8 +5,9 @@ use SilverStripe\Dev\Deprecation; use SilverStripe\Forms\DropdownField; use SilverStripe\ORM\DataList; +use SilverStripe\Forms\HasOneRelationFieldInterface; -class SearchableDropdownField extends DropdownField +class SearchableDropdownField extends DropdownField implements HasOneRelationFieldInterface { use SearchableDropdownTrait; diff --git a/tests/php/Forms/RequiredFieldsTest.php b/tests/php/Forms/RequiredFieldsTest.php index 7da79eda213..afc0468513d 100644 --- a/tests/php/Forms/RequiredFieldsTest.php +++ b/tests/php/Forms/RequiredFieldsTest.php @@ -5,6 +5,7 @@ use SilverStripe\Dev\SapphireTest; use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\Form; +use SilverStripe\Forms\SearchableDropdownField; use SilverStripe\Forms\TreeDropdownField; use SilverStripe\Security\Group; @@ -289,17 +290,27 @@ public function testFieldIsRequired() ); } - public function testTreedropFieldValidation() + public function testHasOneRelationFieldInterfaceValidation() { - $form = new Form(); - $field = new TreeDropdownField('TestField', 'TestField', Group::class); - $form->Fields()->push($field); - $validator = new RequiredFields('TestField'); - $validator->setForm($form); - // blank string and '0' are fail required field validation - $this->assertFalse($validator->php(['TestField' => ''])); - $this->assertFalse($validator->php(['TestField' => '0'])); - // '1' passes required field validation - $this->assertTrue($validator->php(['TestField' => '1'])); + $fieldClasses = [TreeDropdownField::class, SearchableDropdownField::class]; + $suffixes = ['', 'ID']; + foreach ($fieldClasses as $fieldClass) { + foreach ($suffixes as $suffix) { + $form = new Form(); + $param = $fieldClass === TreeDropdownField::class ? Group::class : Group::get(); + $field = new $fieldClass('TestField' . $suffix, 'TestField', $param); + $form->Fields()->push($field); + $validator = new RequiredFields('TestField'); + $validator->setForm($form); + // blank string and 0 and '0' and array with value of 0 fail required field validation + $this->assertFalse($validator->php(['TestField' => ''])); + $this->assertFalse($validator->php(['TestField' => 0])); + $this->assertFalse($validator->php(['TestField' => '0'])); + $this->assertFalse($validator->php(['TestField' => ['value' => 0]])); + $this->assertFalse($validator->php(['TestField' => ['value' => '0']])); + // '1' passes required field validation + $this->assertTrue($validator->php(['TestField' => '1'])); + } + } } }