Skip to content

Commit b55d3c2

Browse files
committed
Validator: do not require BaseControl instance for filled and blank validation
1 parent 56b6ed7 commit b55d3c2

File tree

2 files changed

+85
-4
lines changed

2 files changed

+85
-4
lines changed

src/Forms/Validator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,23 @@ public static function validateStatic(IControl $control, bool $arg): bool
131131
/**
132132
* Is control filled?
133133
*/
134-
public static function validateFilled(Controls\BaseControl $control): bool
134+
public static function validateFilled(IControl $control): bool
135135
{
136-
return $control->isFilled();
136+
if ($control instanceof Controls\BaseControl) {
137+
return $control->isFilled();
138+
}
139+
140+
$value = $control->getValue();
141+
return $value !== null && $value !== [] && $value !== '';
137142
}
138143

139144

140145
/**
141146
* Is control not filled?
142147
*/
143-
public static function validateBlank(Controls\BaseControl $control): bool
148+
public static function validateBlank(IControl $control): bool
144149
{
145-
return !$control->isFilled();
150+
return !static::validateFilled($control);
146151
}
147152

148153

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Forms\Controls\BaseControl
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Forms\Form;
10+
use Nette\Forms\Validator;
11+
use Tester\Assert;
12+
13+
14+
require __DIR__ . '/../bootstrap.php';
15+
16+
17+
class CustomControl implements \Nette\Forms\IControl
18+
{
19+
20+
private $value;
21+
22+
23+
public function __construct($value)
24+
{
25+
$this->value = $value;
26+
}
27+
28+
29+
public function setValue($value)
30+
{
31+
$this->value = $value;
32+
}
33+
34+
35+
public function getValue()
36+
{
37+
return $this->value;
38+
}
39+
40+
41+
public function validate(): void
42+
{
43+
}
44+
45+
46+
public function getErrors(): array
47+
{
48+
return [];
49+
}
50+
51+
52+
public function isOmitted(): bool
53+
{
54+
return false;
55+
}
56+
57+
}
58+
59+
60+
test(function () { // filled, blank
61+
$input = new CustomControl('');
62+
Assert::false(Validator::validateFilled($input));
63+
Assert::true(Validator::validateBlank($input));
64+
65+
$input = new CustomControl(null);
66+
Assert::false(Validator::validateFilled($input));
67+
Assert::true(Validator::validateBlank($input));
68+
69+
$input = new CustomControl([]);
70+
Assert::false(Validator::validateFilled($input));
71+
Assert::true(Validator::validateBlank($input));
72+
73+
$input = new CustomControl(42);
74+
Assert::true(Validator::validateFilled($input));
75+
Assert::false(Validator::validateBlank($input));
76+
});

0 commit comments

Comments
 (0)