Skip to content

Commit 1592cf6

Browse files
committed
breaks exporting rules to JS after non-static validator [Closes #259]
1 parent 8cab472 commit 1592cf6

File tree

8 files changed

+65
-3
lines changed

8 files changed

+65
-3
lines changed

src/Forms/Controls/TextBase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ protected function getRenderedValue(): ?string
137137
/** @return static */
138138
public function addRule($validator, $errorMessage = null, $arg = null)
139139
{
140+
foreach ($this->getRules() as $rule) {
141+
if (!$rule->canExport() && !$rule->branch) {
142+
return $this;
143+
}
144+
}
145+
140146
if ($validator === Form::LENGTH || $validator === Form::MAX_LENGTH) {
141147
$tmp = is_array($arg) ? $arg[1] : $arg;
142148
if (is_scalar($tmp)) {

src/Forms/Controls/TextInput.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public function getControl(): Nette\Utils\Html
6868
/** @return static */
6969
public function addRule($validator, $errorMessage = null, $arg = null)
7070
{
71+
foreach ($this->getRules() as $rule) {
72+
if (!$rule->canExport() && !$rule->branch) {
73+
return $this;
74+
}
75+
}
76+
7177
if ($this->control->type === null && in_array($validator, [Form::EMAIL, Form::URL, Form::INTEGER], true)) {
7278
static $types = [Form::EMAIL => 'email', Form::URL => 'url', Form::INTEGER => 'number'];
7379
$this->control->type = $types[$validator];

src/Forms/Controls/UploadControl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function __construct($label = null, bool $multiple = false)
3333
$this->control->type = 'file';
3434
$this->control->multiple = $multiple;
3535
$this->setOption('type', 'file');
36-
$this->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
36+
$this->addCondition(true) // not to block the export of rules to JS
37+
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
3738
$this->addRule(Form::MAX_FILE_SIZE, null, Forms\Helpers::iniGetSize('upload_max_filesize'));
3839

3940
$this->monitor(Form::class, function (Form $form): void {

src/Forms/Helpers.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ public static function exportRules(Rules $rules): array
100100
{
101101
$payload = [];
102102
foreach ($rules as $rule) {
103-
if (!is_string($op = $rule->validator)) {
104-
if (!Nette\Utils\Callback::isStatic($op)) {
103+
if (!$rule->canExport()) {
104+
if ($rule->branch) {
105105
continue;
106106
}
107+
break;
108+
}
109+
110+
$op = $rule->validator;
111+
if (!is_string($op)) {
107112
$op = Nette\Utils\Callback::toString($op);
108113
}
114+
109115
if ($rule->branch) {
110116
$item = [
111117
'op' => ($rule->isNegative ? '~' : '') . $op,

src/Forms/Rule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,12 @@ class Rule
3636

3737
/** @var Rules|null for conditions */
3838
public $branch;
39+
40+
41+
/** @internal */
42+
public function canExport(): bool
43+
{
44+
return is_string($this->validator)
45+
|| Nette\Utils\Callback::isStatic($this->validator);
46+
}
3947
}

tests/Forms/Controls.TextArea.render.phpt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,13 @@ test('setEmptyValue & setNullable', function () {
119119
Assert::null($input->getValue());
120120
Assert::same('<textarea name="text" id="frm-text" data-nette-empty-value="empty">empty </textarea>', (string) $input->getControl());
121121
});
122+
123+
124+
test('addFilter() & rules', function () {
125+
$form = new Form;
126+
$input = $form->addTextArea('text')
127+
->addFilter(function () {})
128+
->addRule(Form::MAX_LENGTH, 'maxl', 10);
129+
130+
Assert::same('<textarea name="text" id="frm-text"></textarea>', (string) $input->getControl());
131+
});

tests/Forms/Controls.TextInput.render.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,15 @@ test('addInteger', function () {
235235

236236
Assert::same('<input type="number" name="text" id="frm-text" data-nette-rules=\'[{"op":":integer","msg":"Please enter a valid integer."}]\'>', (string) $input->getControl());
237237
});
238+
239+
240+
test('addFilter() & rules', function () {
241+
$form = new Form;
242+
$input = $form->addText('text')
243+
->addRule(Form::MIN, 'min', 1)
244+
->addFilter(function () {})
245+
->addRule(Form::MAX, 'max', 10)
246+
->addRule(Form::MAX_LENGTH, 'maxl', 10);
247+
248+
Assert::same('<input type="text" name="text" id="frm-text" data-nette-rules=\'[{"op":":min","msg":"min","arg":1}]\'>', (string) $input->getControl());
249+
});

tests/Forms/Helpers.exportRules.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,16 @@ test('', function () {
9191
],
9292
], Helpers::exportRules($input2->getRules()));
9393
});
94+
95+
96+
test('addFilter', function () {
97+
$form = new Form;
98+
$input = $form->addText('text');
99+
$input->addRule(Form::PATTERN, 'match pattern', '\d+');
100+
$input->addFilter(function () {});
101+
$input->setRequired(false);
102+
$input->addRule(Form::EMAIL);
103+
Assert::same([
104+
['op' => ':pattern', 'msg' => 'match pattern', 'arg' => '\\d+'],
105+
], Helpers::exportRules($input->getRules()));
106+
});

0 commit comments

Comments
 (0)