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
12 changes: 6 additions & 6 deletions src/Field/Definition/ChoiceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private function validateSingleSelect (ComponentContext $context, array $content
],
);

\assert(null === $data || \is_int($data) || \is_string($data));
\assert(\is_int($data) || \is_string($data));
keichinger marked this conversation as resolved.
Show resolved Hide resolved

if (\is_string($data))
{
Expand Down Expand Up @@ -138,7 +138,7 @@ private function validateMultiSelect (ComponentContext $context, array $contentP
$this,
$data,
[
new Type("Array"),
new Type("array"),
keichinger marked this conversation as resolved.
Show resolved Hide resolved
new All([
new NotNull(),
new AtLeastOneOf([
Expand All @@ -149,17 +149,17 @@ private function validateMultiSelect (ComponentContext $context, array $contentP
],
);

\assert(null === $data || \is_array($data));
\assert(\is_array($data));
keichinger marked this conversation as resolved.
Show resolved Hide resolved

if (null !== $this->minimumNumberOfOptions || null !== $this->maximumNumberOfOptions)
if ($this->required || null !== $this->minimumNumberOfOptions || null !== $this->maximumNumberOfOptions)
{
$context->ensureDataIsValid(
$contentPath,
$this,
$data,
[
new Count(
min: $this->minimumNumberOfOptions,
min: $this->minimumNumberOfOptions ?? ($this->required ? 1 : null),
max: $this->maximumNumberOfOptions,
minMessage: "At least {{ limit }} option(s) must be selected.",
maxMessage: "You cannot specify more than {{ limit }} options.",
Expand All @@ -170,7 +170,7 @@ private function validateMultiSelect (ComponentContext $context, array $contentP

$choicesConstraints = $this->choices->getValidationConstraints(true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I see that: I really dislike this API. We need to change it in the next major version 😅


if (null !== $data && !empty($choicesConstraints))
if (!empty($choicesConstraints))
{
$context->ensureDataIsValid(
$contentPath,
Expand Down