Skip to content

Commit 550f347

Browse files
author
Michal Kleiner
committed
Merge branch '5' into 6.0
2 parents 71e4139 + 80ecb7e commit 550f347

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/Fields/MultiValueCheckboxField.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ public function saveInto(DataObjectInterface $record)
208208
}
209209
}
210210

211+
/**
212+
* Load the value from the dataobject into this field
213+
*
214+
* @param DataObject|DataObjectInterface $record
215+
*/
216+
public function loadFrom(DataObjectInterface $record)
217+
{
218+
$fieldName = $this->getName();
219+
if (empty($fieldName) || empty($record)) {
220+
return;
221+
}
222+
223+
if ($record->hasField($fieldName)) {
224+
$value = $record->$fieldName;
225+
226+
parent::setValue($value);
227+
}
228+
}
229+
211230
/**
212231
* Return the CheckboxSetField value as a string
213232
* selected item keys.

tests/MultiValueCheckboxFieldTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Symbiote\MultiValueField\Tests;
4+
5+
use SilverStripe\Dev\SapphireTest;
6+
use Symbiote\MultiValueField\Fields\MultiValueCheckboxField;
7+
8+
class MultiValueCheckboxFieldTest extends SapphireTest
9+
{
10+
protected static $extra_dataobjects = [
11+
MultiValueFieldTest_DataObject::class
12+
];
13+
14+
public function testLoadFrom()
15+
{
16+
$obj = new MultiValueFieldTest_DataObject();
17+
$obj->MVField = ['One', 'Two'];
18+
$field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']);
19+
$field->loadFrom($obj);
20+
$this->assertEquals('One,Two', $field->dataValue());
21+
}
22+
23+
public function testSetValue()
24+
{
25+
$field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']);
26+
$field->setValue(['One', 'Two']);
27+
$this->assertEquals('One,Two', $field->dataValue());
28+
$obj = new MultiValueFieldTest_DataObject();
29+
$obj->MVField = ['Three', 'Four'];
30+
$field->setValue('', $obj);
31+
$this->assertEquals('Three,Four', $field->dataValue());
32+
}
33+
}

0 commit comments

Comments
 (0)