Merge pull request #684 from cosmocode/clear-multi-media

Clear multi field of type media
This commit is contained in:
Anna Dabrowska
2023-11-15 09:18:11 +01:00
committed by GitHub
2 changed files with 18 additions and 1 deletions

View File

@ -6,6 +6,7 @@ use dokuwiki\plugin\struct\meta\Column;
use dokuwiki\plugin\struct\test\mock\Assignments;
use dokuwiki\plugin\struct\test\mock\Lookup;
use dokuwiki\plugin\struct\types\Decimal;
use dokuwiki\plugin\struct\types\Media;
use dokuwiki\plugin\struct\types\Text;
/**
@ -105,6 +106,17 @@ class ValidatorTest extends StructTest
$validator->validateValue($col, $value);
$this->assertEquals([''], $value);
// some fields like media or date can post an array with multiple empty strings
// because they use multiple inputs instead of comma separation in one input
$media = new Media(null, '', true);
$col = new Column(10, $media);
$validator = new mock\ValueValidator();
$value = ['', '', ''];
$validator->validateValue($col, $value);
$this->assertEquals([''], $value);
}
}

View File

@ -42,9 +42,14 @@ class ValueValidator
}
// strip empty fields from multi vals
// but keep at least one so we can properly delete multivalues on update
if (is_array($rawvalue) && count($rawvalue) > 1) {
// some fields like media or date can post an array with multiple empty strings
// because they use multiple inputs instead of comma separation in one input
if (is_array($rawvalue)) {
$rawvalue = array_filter($rawvalue, [$this, 'filter']);
$rawvalue = array_values($rawvalue); // reset the array keys
if (empty($rawvalue)) {
$rawvalue = [''];
}
}
// validate data