Skip to content

Commit 1d87dba

Browse files
committed
improved phpDoc
added @Property $name to suppress deprecation from nette/component-model
1 parent a6aedf4 commit 1d87dba

8 files changed

Lines changed: 73 additions & 11 deletions

File tree

src/Forms/Container.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
/**
1717
* Container for form controls.
1818
*
19+
* @property-read string $name
1920
* @property ArrayHash<mixed> $values
2021
* @property-read \Iterator $controls
2122
* @property-read ?Form $form

src/Forms/Controls/BaseControl.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
/**
2020
* Base implementation for form controls with HTML rendering, validation, translation, and option support.
2121
*
22+
* @property-read string $name
2223
* @property-read Form $form
2324
* @property-read string $htmlName
2425
* @property string|bool|null $htmlId
@@ -391,7 +392,7 @@ public function translate(mixed $value, mixed ...$parameters): mixed
391392

392393
/**
393394
* Adds a validation rule.
394-
* @param (callable(Control): bool)|string $validator
395+
* @param (callable(Control, mixed): bool)|string $validator
395396
* @return static
396397
*/
397398
public function addRule(
@@ -406,7 +407,7 @@ public function addRule(
406407

407408
/**
408409
* Adds a validation condition and returns a new branch.
409-
* @param (callable(Control): bool)|string|bool $validator
410+
* @param (callable(Control, mixed): bool)|string|bool $validator
410411
*/
411412
public function addCondition($validator, mixed $value = null): Rules
412413
{
@@ -416,7 +417,7 @@ public function addCondition($validator, mixed $value = null): Rules
416417

417418
/**
418419
* Adds a validation condition based on another control and returns a new branch.
419-
* @param (callable(Control): bool)|string $validator
420+
* @param (callable(Control, mixed): bool)|string $validator
420421
*/
421422
public function addConditionOn(Control $control, $validator, mixed $value = null): Rules
422423
{

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: 4 additions & 4 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
{

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)