Skip to content

Commit

Permalink
Fix consistency with min trait and array
Browse files Browse the repository at this point in the history
  • Loading branch information
afbora committed Feb 10, 2025
1 parent b92b57d commit ddf7f75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Form/Mixin/Min.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@ trait Min

public function min(): int|null
{
// set min to at least 1, if required
if ($this->required === true) {
return $this->min ?? 1;
}

return $this->min;
}

protected function setMin(int|null $min = null)
{
$this->min = $min;
}

public function isRequired(): bool
{
// set required to true if min is set
if ($this->min) {
return true;
}

return $this->required;
}
}
5 changes: 5 additions & 0 deletions tests/Form/Field/EntriesFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public function testMin()
]);

$this->assertSame(3, $field->min());
$this->assertTrue($field->isRequired());
$this->assertFalse($field->isValid());
$this->assertSame($field->errors()['entries'], 'You must add at least 3 entries');
}
Expand All @@ -190,6 +191,7 @@ public function testMinValid()
]);

$this->assertSame(2, $field->min());
$this->assertTrue($field->isRequired());
$this->assertTrue($field->isValid());
}

Expand All @@ -203,6 +205,7 @@ public function testMinInvalid()
]);

$this->assertSame(3, $field->min());
$this->assertTrue($field->isRequired());
$this->assertFalse($field->isValid());
$this->assertSame($field->errors()['entries'], 'You must add at least 3 entries');
}
Expand All @@ -217,6 +220,7 @@ public function testRequiredValid()
]);

$this->assertTrue($field->isValid());
$this->assertSame(1, $field->min());
}

public function testRequiredInvalid()
Expand All @@ -226,6 +230,7 @@ public function testRequiredInvalid()
]);

$this->assertFalse($field->isValid());
$this->assertSame(1, $field->min());
}

public static function supportsProvider(): array
Expand Down

0 comments on commit ddf7f75

Please sign in to comment.