Skip to content

Commit 816f3df

Browse files
committed
Rules::setRequired() accepts objects as addRule()
1 parent 8435f4b commit 816f3df

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Forms/Rules.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct(IControl $control)
5050
public function setRequired($value = TRUE)
5151
{
5252
if ($value) {
53-
$this->addRule(Form::REQUIRED, is_string($value) ? $value : NULL);
53+
$this->addRule(Form::REQUIRED, $value === TRUE ? NULL : $value);
5454
} else {
5555
$this->required = NULL;
5656
}

tests/Forms/Controls.translate().phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,18 @@ test(function () {
8282
$form->setTranslator(new Translator);
8383

8484
$email = $form->addText('email')
85+
->setRequired('error')
8586
->addRule($form::EMAIL, 'error');
86-
Assert::match('<input type="email" name="email" id="frm-email" data-nette-rules=\'[{"op":":email","msg":"error"}]\'>', (string) $email->getControl());
87+
88+
Assert::match('<input type="email" name="email" id="frm-email" required data-nette-rules=\'[{"op":":filled","msg":"error"},{"op":":email","msg":"error"}]\'>', (string) $email->getControl());
8789
$email->validate();
8890
Assert::same(['error'], $email->getErrors());
8991

9092
$email2 = $form->addText('email2')
93+
->setRequired(new StringWrapper('Your name'))
9194
->addRule($form::EMAIL, new StringWrapper('Your name'));
92-
Assert::match('<input type="email" name="email2" id="frm-email2" data-nette-rules=\'[{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
95+
96+
Assert::match('<input type="email" name="email2" id="frm-email2" required data-nette-rules=\'[{"op":":filled","msg":"StringWrapper"},{"op":":email","msg":"StringWrapper"}]\'>', (string) $email2->getControl());
9397
$email2->validate();
9498
Assert::same(['StringWrapper'], $email2->getErrors());
9599
});

0 commit comments

Comments
 (0)