Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting minimum and maximum count of selected #96

Merged
merged 12 commits into from
Oct 30, 2023
Merged
17 changes: 13 additions & 4 deletions src/Field/Definition/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,23 +78,29 @@ protected function toManagementApiData () : array

/**
* @inheritDoc
*
* @param string|int|null|array<string|int|null> $data
*/
public function validateData (ComponentContext $context, array $contentPath, mixed $data, array $fullData) : void
{
if ($this->allowMultiselect)
{
\assert(null === $data || \is_array($data));
keichinger marked this conversation as resolved.
Show resolved Hide resolved

$this->validateMultiSelect($context, $contentPath, $data);
}
else
{
\assert(null === $data || \is_string($data) || \is_int($data));

$this->validateSingleSelect($context, $contentPath, $data);
}
}


private function validateSingleSelect (ComponentContext $context, array $contentPath, mixed $data) : void
private function validateSingleSelect (ComponentContext $context, array $contentPath, string|int|null $data) : void
{
$data = $context->normalizeOptionalString($data);
$data = $context->normalizeOptionalString((string) $data);

if (null === $data)
{
Expand Down Expand Up @@ -129,7 +135,10 @@ private function validateSingleSelect (ComponentContext $context, array $content
}


private function validateMultiSelect (ComponentContext $context, array $contentPath, mixed $data) : void
/**
* @param array<int|string|null>|null $data
*/
private function validateMultiSelect (ComponentContext $context, array $contentPath, array|null $data) : void
{
if (null === $data)
{
Expand All @@ -139,7 +148,7 @@ private function validateMultiSelect (ComponentContext $context, array $contentP
\assert(\is_array($data));

$data = \array_map(
$context->normalizeOptionalString(...),
static fn (mixed $value) => $context->normalizeOptionalString((string) $value),
$data,
);

Expand Down
Loading