mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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:
commit
0c8fcfb54c
@ -112,9 +112,14 @@ class RequiredFields extends Validator
|
||||
if (is_array($value)) {
|
||||
if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
|
||||
$error = true;
|
||||
} else {
|
||||
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;
|
||||
if (is_a($formField, HasOneRelationFieldInterface::class)) {
|
||||
|
@ -5,8 +5,9 @@ namespace SilverStripe\Forms;
|
||||
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;
|
||||
|
||||
|
@ -5,6 +5,7 @@ namespace SilverStripe\Forms\Tests;
|
||||
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 @@ 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();
|
||||
$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']));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user