Skip to content

Commit

Permalink
Fix saving of CheckboxSetMultivalueField
Browse files Browse the repository at this point in the history
Fixes #73
  • Loading branch information
xini authored and Michal Kleiner committed May 21, 2023
1 parent 8538c44 commit 02742df
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Fields/MultiValueCheckboxField.php
Expand Up @@ -208,6 +208,25 @@ public function saveInto(DataObjectInterface $record)
}
}

/**
* Load the value from the dataobject into this field
*
* @param DataObject|DataObjectInterface $record
*/
public function loadFrom(DataObjectInterface $record)
{
$fieldName = $this->getName();
if (empty($fieldName) || empty($record)) {
return;
}

if ($record->hasField($fieldName)) {
$value = $record->$fieldName;

parent::setValue($value);
}
}

/**
* Return the CheckboxSetField value as a string
* selected item keys.
Expand Down
33 changes: 33 additions & 0 deletions tests/MultiValueCheckboxFieldTest.php
@@ -0,0 +1,33 @@
<?php

namespace Symbiote\MultiValueField\Tests;

use SilverStripe\Dev\SapphireTest;
use Symbiote\MultiValueField\Fields\MultiValueCheckboxField;

class MultiValueCheckboxFieldTest extends SapphireTest
{
protected static $extra_dataobjects = [
MultiValueFieldTest_DataObject::class
];

public function testLoadFrom()
{
$obj = new MultiValueFieldTest_DataObject();
$obj->MVField = ['One', 'Two'];
$field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']);
$field->loadFrom($obj);
$this->assertEquals('One,Two', $field->dataValue());
}

public function testSetValue()
{
$field = new MultiValueCheckboxField('MVField', 'MVField', ['One', 'Two', 'Three', 'Four']);
$field->setValue(['One', 'Two']);
$this->assertEquals('One,Two', $field->dataValue());
$obj = new MultiValueFieldTest_DataObject();
$obj->MVField = ['Three', 'Four'];
$field->setValue('', $obj);
$this->assertEquals('Three,Four', $field->dataValue());
}
}

0 comments on commit 02742df

Please sign in to comment.