From b8f0b8ca4f72a12f22e5c4d8d91d04be276a3439 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Thu, 2 May 2024 12:06:43 +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 | 26 +++++++++++++++++++++++--- 3 files changed, 31 insertions(+), 5 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..5c080cfc9f9 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,16 +290,35 @@ public function testFieldIsRequired() ); } - public function testTreedropFieldValidation() + public function provideHasOneRelationFieldInterfaceValidation(): array + { + return [ + [ + 'className' => TreeDropdownField::class, + ], + [ + 'className' => SearchableDropdownField::class, + ] + ]; + } + + /** + * @dataProvider provideHasOneRelationFieldInterfaceValidation + */ + public function testHasOneRelationFieldInterfaceValidation(string $className) { $form = new Form(); - $field = new TreeDropdownField('TestField', 'TestField', Group::class); + $param = $className === TreeDropdownField::class ? Group::class : Group::get(); + $field = new $className('TestField', 'TestField', $param); $form->Fields()->push($field); $validator = new RequiredFields('TestField'); $validator->setForm($form); - // blank string and '0' are fail required field validation + // 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'])); }