Merge pull request #11213 from creative-commoners/pulls/5/required-has-one

FIX Handle getting HasOneRelationFieldInterface passed as an array
This commit is contained in:
Guy Sartorelli 2024-05-07 13:01:59 +12:00 committed by GitHub
commit 0c8fcfb54c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 5 deletions

View File

@ -112,9 +112,14 @@ class RequiredFields extends Validator
if (is_array($value)) { if (is_array($value)) {
if ($formField instanceof FileField && isset($value['error']) && $value['error']) { if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
$error = true; $error = true;
} else {
if (is_a($formField, HasOneRelationFieldInterface::class) && isset($value['value'])) {
$stringValue = (string) $value['value'];
$error = in_array($stringValue, ['0', '']);
} else { } else {
$error = (count($value ?? [])) ? false : true; $error = (count($value ?? [])) ? false : true;
} }
}
} else { } else {
$stringValue = (string) $value; $stringValue = (string) $value;
if (is_a($formField, HasOneRelationFieldInterface::class)) { if (is_a($formField, HasOneRelationFieldInterface::class)) {

View File

@ -5,8 +5,9 @@ namespace SilverStripe\Forms;
use SilverStripe\Dev\Deprecation; use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\DropdownField; use SilverStripe\Forms\DropdownField;
use SilverStripe\ORM\DataList; use SilverStripe\ORM\DataList;
use SilverStripe\Forms\HasOneRelationFieldInterface;
class SearchableDropdownField extends DropdownField class SearchableDropdownField extends DropdownField implements HasOneRelationFieldInterface
{ {
use SearchableDropdownTrait; use SearchableDropdownTrait;

View File

@ -5,6 +5,7 @@ namespace SilverStripe\Forms\Tests;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\RequiredFields; use SilverStripe\Forms\RequiredFields;
use SilverStripe\Forms\Form; use SilverStripe\Forms\Form;
use SilverStripe\Forms\SearchableDropdownField;
use SilverStripe\Forms\TreeDropdownField; use SilverStripe\Forms\TreeDropdownField;
use SilverStripe\Security\Group; use SilverStripe\Security\Group;
@ -289,16 +290,35 @@ class RequiredFieldsTest extends SapphireTest
); );
} }
public function testTreedropFieldValidation() public function provideHasOneRelationFieldInterfaceValidation(): array
{
return [
[
'className' => TreeDropdownField::class,
],
[
'className' => SearchableDropdownField::class,
]
];
}
/**
* @dataProvider provideHasOneRelationFieldInterfaceValidation
*/
public function testHasOneRelationFieldInterfaceValidation(string $className)
{ {
$form = new Form(); $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); $form->Fields()->push($field);
$validator = new RequiredFields('TestField'); $validator = new RequiredFields('TestField');
$validator->setForm($form); $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' => '']));
$this->assertFalse($validator->php(['TestField' => 0]));
$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 // '1' passes required field validation
$this->assertTrue($validator->php(['TestField' => '1'])); $this->assertTrue($validator->php(['TestField' => '1']));
} }