Merge pull request #162 from tractorcow/pulls/fix-react

This commit is contained in:
Guy Marriott 2020-07-06 16:55:48 -07:00 committed by GitHub
commit 66aa1af72a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 2 deletions

View File

@ -6,7 +6,6 @@ use Exception;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Forms\MultiSelectField;
use SilverStripe\Forms\Validator;
@ -319,7 +318,15 @@ class TagField extends MultiSelectField
return parent::setValue($value);
}
return parent::setValue(array_filter($value));
// Safely map php / react-select values to flat list
$values = [];
foreach ($value as $item) {
if ($item) {
$values[] = isset($item['Value']) ? $item['Value'] : $item;
}
}
return parent::setValue($values);
}
/**

View File

@ -131,6 +131,30 @@ class TagFieldTest extends SapphireTest
$record
);
}
public function testSavesReactTags()
{
$record = $this->getNewTagFieldTestBlogPost('BlogPost1');
$record->write();
$field = new TagField('Tags', '', new DataList(TagFieldTestBlogTag::class));
$field->setValue([
[
'Title' => 'Tag1',
'Value' => 'Tag1',
],
[
'Title' => 'Tag2',
'Value' => 'Tag2',
],
]);
$field->saveInto($record);
$this->compareExpectedAndActualTags(
['Tag1', 'Tag2'],
$record
);
}
/**
* Ensure that {@see TagField::saveInto} respects existing tags