Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class MultipleSelectionFormField extends AbstractFormField implements
*/
protected $value = [];

/**
* @since 6.2
*/
private bool $ignoreInvalidValues = false;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -130,13 +135,30 @@ public function value($value)
throw new InvalidFormFieldValue($this, 'array', \gettype($value));
}

$unknownValues = \array_diff($value, \array_keys($this->getOptions()));
if (!empty($unknownValues)) {
throw new \InvalidArgumentException(
"Unknown values '" . \implode("', '", $unknownValues) . "' for field '{$this->getId()}'."
);
$validKeys = \array_keys($this->getOptions());
$unknownValues = \array_diff($value, $validKeys);
if ($unknownValues !== []) {
if ($this->ignoreInvalidValues) {
$value = \array_intersect($value, $validKeys);
} else {
throw new \InvalidArgumentException(
"Unknown values '" . \implode("', '", $unknownValues) . "' for field '{$this->getId()}'."
);
}
}

return parent::value($value);
}

/**
* Ignores invalid values when reading them from data.
*
* @since 6.2
*/
public function ignoreInvalidValues(bool $ignoreInvalidValues = true): self
{
$this->ignoreInvalidValues = $ignoreInvalidValues;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ final class SelectFormField extends AbstractFormField implements
*/
protected $templateName = 'shared_selectFormField';

/**
* @since 6.2
*/
private bool $ignoreInvalidValues = false;

/**
* @inheritDoc
*/
Expand Down Expand Up @@ -75,10 +80,26 @@ public function value($value)
{
if ($value !== null && $value !== '') {
if (!isset($this->getOptions()[$value])) {
throw new \InvalidArgumentException("Unknown value '{$value}' for field '{$this->getId()}'.");
if ($this->ignoreInvalidValues) {
$value = null;
} else {
throw new \InvalidArgumentException("Unknown value '{$value}' for field '{$this->getId()}'.");
}
}
}

return parent::value($value);
}

/**
* Ignores invalid values when reading them from data.
*
* @since 6.2
*/
public function ignoreInvalidValues(bool $ignoreInvalidValues = true): self
{
$this->ignoreInvalidValues = $ignoreInvalidValues;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function getId(): string
#[\Override]
public function getFormField(string $id, array $configuration = []): AbstractFormField
{
$formField = MultipleSelectionFormField::create($id);
$formField = MultipleSelectionFormField::create($id)
->ignoreInvalidValues();
$this->setSelectOptions($formField, $configuration);

return $formField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function getId(): string
#[\Override]
public function getFormField(string $id, array $configuration = []): AbstractFormField
{
$formField = SelectFormField::create($id);
$formField = SelectFormField::create($id)
->ignoreInvalidValues();
$this->setSelectOptions($formField, $configuration);

return $formField;
Expand Down