Skip to content

Commit

Permalink
Merge pull request #87 from xini/fix-checkbox-saving-73
Browse files Browse the repository at this point in the history
Fix saving of CheckboxSetMultivalueField
  • Loading branch information
michalkleiner authored May 21, 2023
2 parents 8538c44 + 02742df commit d1032f3
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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 d1032f3

Please sign in to comment.