Skip to content

Commit 341e8a7

Browse files
committed
Form: checks for missing setRequired before render
1 parent 6d29b93 commit 341e8a7

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/Forms/Form.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,9 @@ protected function beforeRender()
612612
public function fireRenderEvents()
613613
{
614614
if (!$this->beforeRenderCalled) {
615+
foreach ($this->getComponents(TRUE, Controls\BaseControl::class) as $control) {
616+
$control->getRules()->check();
617+
}
615618
$this->beforeRenderCalled = TRUE;
616619
$this->beforeRender();
617620
$this->onRender($this);

src/Forms/Rules.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,29 @@ public function validate($emptyOptional = FALSE)
251251
}
252252

253253

254+
/**
255+
* @internal
256+
*/
257+
public function check()
258+
{
259+
if ($this->required !== NULL) {
260+
return;
261+
}
262+
foreach ($this->rules as $rule) {
263+
if ($rule->control === $this->control && ($rule->validator === Form::FILLED || $rule->validator === Form::BLANK)) {
264+
// ignore
265+
} elseif ($rule->branch) {
266+
if ($rule->branch->check() === TRUE) {
267+
return TRUE;
268+
}
269+
} else {
270+
trigger_error("Missing setRequired(TRUE | FALSE) on field '{$rule->control->getName()}' in form '{$rule->control->getForm()->getName()}'.", E_USER_WARNING);
271+
return TRUE;
272+
}
273+
}
274+
}
275+
276+
254277
/**
255278
* Validates single rule.
256279
* @return bool

tests/Forms/Rules.formatMessage.phpt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ require __DIR__ . '/../bootstrap.php';
1313

1414
$form = new Form;
1515
$form->addText('args1')
16-
->addRule(Form::RANGE, '%d %d', [1, 5]);
16+
->setRequired()
17+
->addRule(Form::RANGE, '%d %d', [1, 5])
18+
->setDefaultValue('x');
1719

1820
$form->addText('args2')
19-
->addRule(Form::RANGE, '%2$d %1$d', [1, 5]);
21+
->setRequired()
22+
->addRule(Form::RANGE, '%2$d %1$d', [1, 5])
23+
->setDefaultValue('x');
2024

2125
$form->addText('args3')
22-
->addRule(Form::LENGTH, '%d %d', 1);
26+
->setRequired()
27+
->addRule(Form::LENGTH, '%d %d', 1)
28+
->setDefaultValue('xyz');
2329

2430
$form->addText('special', 'Label')
31+
->setRequired()
2532
->addRule(Form::EMAIL, '%label %value is invalid [field %name] %d', $form['special'])
2633
->setDefaultValue('xyz');
2734

@@ -41,4 +48,4 @@ Assert::same(['1 '], $form['args3']->getErrors());
4148

4249
Assert::same(['Label xyz is invalid [field special] xyz'], $form['special']->getErrors());
4350

44-
Assert::match('%A%data-nette-rules=\'[{"op":":email","msg":"Label %value is invalid [field special] %0"%A%', $form->__toString(TRUE));
51+
Assert::match('%A%data-nette-rules=\'%A%{"op":":email","msg":"Label %value is invalid [field special] %0"%A%', $form->__toString(TRUE));

0 commit comments

Comments
 (0)