Skip to content

Commit b6952f3

Browse files
committed
improved phpDoc errors
1 parent a50249f commit b6952f3

7 files changed

Lines changed: 73 additions & 13 deletions

File tree

src/Forms/Controls/BaseControl.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public function translate(mixed $value, mixed ...$parameters): mixed
391391

392392
/**
393393
* Adds a validation rule.
394-
* @param (callable(Control): bool)|string $validator
394+
* @param (callable(Control, mixed): bool)|string $validator
395395
* @return static
396396
*/
397397
public function addRule(
@@ -406,7 +406,7 @@ public function addRule(
406406

407407
/**
408408
* Adds a validation condition and returns a new branch.
409-
* @param (callable(Control): bool)|string|bool $validator
409+
* @param (callable(Control, mixed): bool)|string|bool $validator
410410
*/
411411
public function addCondition($validator, mixed $value = null): Rules
412412
{
@@ -416,7 +416,7 @@ public function addCondition($validator, mixed $value = null): Rules
416416

417417
/**
418418
* Adds a validation condition based on another control and returns a new branch.
419-
* @param (callable(Control): bool)|string $validator
419+
* @param (callable(Control, mixed): bool)|string $validator
420420
*/
421421
public function addConditionOn(Control $control, $validator, mixed $value = null): Rules
422422
{

src/Forms/Controls/TextBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function getRenderedValue(): ?string
124124

125125

126126
/**
127-
* @param (callable(Nette\Forms\Control): bool)|string $validator
127+
* @param (callable(Nette\Forms\Control, mixed): bool)|string $validator
128128
* @return static
129129
*/
130130
public function addRule(

src/Forms/Controls/TextInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getControl(): Nette\Utils\Html
6161

6262

6363
/**
64-
* @param (callable(Nette\Forms\Control): bool)|string $validator
64+
* @param (callable(Nette\Forms\Control, mixed): bool)|string $validator
6565
* @return static
6666
*/
6767
public function addRule(

src/Forms/Controls/UploadControl.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function isOk(): bool
125125

126126

127127
/**
128-
* @param (callable(Nette\Forms\Control): bool)|string $validator
128+
* @param (callable(Nette\Forms\Control, mixed): bool)|string $validator
129129
* @return static
130130
*/
131131
public function addRule(

src/Forms/Rule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ final class Rule
1919
{
2020
public Control $control;
2121

22-
/** @var (callable(Control): bool)|string */
22+
/** @var (callable(Control, mixed): bool)|string */
2323
public mixed $validator;
2424
public mixed $arg = null;
2525
public bool $isNegative = false;

src/Forms/Rules.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function isRequired(): bool
6565

6666
/**
6767
* Adds a validation rule for the current control.
68-
* @param (callable(Control): bool)|string $validator
68+
* @param (callable(Control, mixed): bool)|string $validator
6969
*/
7070
public function addRule(
7171
callable|string $validator,
@@ -95,7 +95,7 @@ public function addRule(
9595

9696
/**
9797
* Removes a validation rule for the current control.
98-
* @param (callable(Control): bool)|string $validator
98+
* @param (callable(Control, mixed): bool)|string $validator
9999
*/
100100
public function removeRule(callable|string $validator): static
101101
{
@@ -115,7 +115,7 @@ public function removeRule(callable|string $validator): static
115115

116116
/**
117117
* Adds a validation condition and returns new branch.
118-
* @param (callable(Control): bool)|string|bool $validator
118+
* @param (callable(Control, mixed): bool)|string|bool $validator
119119
*/
120120
public function addCondition(callable|string|bool $validator, mixed $arg = null): static
121121
{
@@ -132,7 +132,7 @@ public function addCondition(callable|string|bool $validator, mixed $arg = null)
132132

133133
/**
134134
* Adds a validation condition on a specified control and returns new branch.
135-
* @param (callable(Control): bool)|string $validator
135+
* @param (callable(Control, mixed): bool)|string $validator
136136
*/
137137
public function addConditionOn(Control $control, callable|string $validator, mixed $arg = null): static
138138
{
@@ -230,11 +230,11 @@ public function getToggleStates(array $toggles = [], bool $success = true, ?bool
230230
if ($rule->branch) {
231231
$toggles = $rule->branch->getToggleStates(
232232
$toggles,
233-
$success && static::validateRule($rule),
233+
$success && Rules::validateRule($rule),
234234
$rule->validator === Form::Blank ? false : $emptyOptional,
235235
);
236236
} elseif (!$emptyOptional || $rule->validator === Form::Filled) {
237-
$success = $success && static::validateRule($rule);
237+
$success = $success && Rules::validateRule($rule);
238238
}
239239
}
240240

tests/types/forms-types.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,63 @@ function testFormEvents(Form $form): void
5050
assertType(Form::class, $form);
5151
};
5252
}
53+
54+
55+
// Issue #350: addRule() must accept validators with a second $arg parameter
56+
function testAddRuleValidatorOneParam(Form $form): void
57+
{
58+
$form->addText('field')
59+
->addRule(fn(Nette\Forms\Control $input): bool => (bool) $input->getValue());
60+
}
61+
62+
63+
function testAddRuleValidatorTwoParams(Form $form): void
64+
{
65+
$form->addInteger('num')
66+
->addRule(
67+
fn(Nette\Forms\Control $input, mixed $arg): bool => $input->getValue() > $arg,
68+
'Must be greater than %d',
69+
0,
70+
);
71+
}
72+
73+
74+
function testAddRuleStaticCallableWithArg(Form $form): void
75+
{
76+
$form->addInteger('num')
77+
->addRule(
78+
[CustomValidators::class, 'validateDivisibility'],
79+
'Must be divisible by %d',
80+
8,
81+
);
82+
}
83+
84+
85+
// addCondition() with one-parameter handler
86+
function testAddConditionOneParam(Form $form): void
87+
{
88+
$form->addText('field')
89+
->addCondition(fn(Nette\Forms\Control $input): bool => (bool) $input->getValue())
90+
->setRequired();
91+
}
92+
93+
94+
// addCondition() with two-parameter handler
95+
function testAddConditionTwoParams(Form $form): void
96+
{
97+
$form->addInteger('num')
98+
->addCondition(
99+
fn(Nette\Forms\Control $input, mixed $arg): bool => $input->getValue() > $arg,
100+
0,
101+
)
102+
->setRequired();
103+
}
104+
105+
106+
class CustomValidators
107+
{
108+
public static function validateDivisibility(Nette\Forms\Control $input, mixed $arg): bool
109+
{
110+
return $input->getValue() % $arg === 0;
111+
}
112+
}

0 commit comments

Comments
 (0)