Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Handle getting HasOneRelationFieldInterface passed as an array #11213

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Forms/RequiredFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
$stringValue = (string) $value;
Expand Down
3 changes: 2 additions & 1 deletion src/Forms/SearchableDropdownField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
26 changes: 23 additions & 3 deletions tests/php/Forms/RequiredFieldsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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']));
}
Expand Down
Loading