diff --git a/src/Forms/RequiredFields.php b/src/Forms/RequiredFields.php index 918a32a1121..9b684cc63ce 100644 --- a/src/Forms/RequiredFields.php +++ b/src/Forms/RequiredFields.php @@ -104,10 +104,18 @@ public function php($data) $fieldName = $fieldName->getName(); } else { $formField = $fields->dataFieldByName($fieldName); + if (is_null($formField)) { + $formField = $fields->dataFieldByName($fieldName . 'ID'); + } } // submitted data for file upload fields come back as an array - $value = isset($data[$fieldName]) ? $data[$fieldName] : null; + $value = null; + if (isset($data[$fieldName])) { + $value = $data[$fieldName]; + } elseif (isset($data[$fieldName . 'ID'])) { + $value = $data[$fieldName . 'ID']; + } if (is_array($value)) { if ($formField instanceof FileField && isset($value['error']) && $value['error']) { 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..997c826725f 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,24 @@ 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' 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'])); + } + } } }