|
6 | 6 |
|
7 | 7 | declare(strict_types=1);
|
8 | 8 |
|
| 9 | +use Nette\Forms\Controls\TextInput; |
9 | 10 | use Nette\Forms\Form;
|
10 | 11 | use Tester\Assert;
|
11 | 12 |
|
@@ -34,11 +35,36 @@ $form->addText('special', 'Label:')
|
34 | 35 | ->addRule(Form::EMAIL, '%label %value is invalid [field %name] %d', $form['special'])
|
35 | 36 | ->setDefaultValue('xyz');
|
36 | 37 |
|
| 38 | +$form->addText('function', 'Label:') |
| 39 | + ->setRequired() |
| 40 | + ->addRule(function(TextInput $input, string $arg) { |
| 41 | + return strpos($input->getValue(), $arg) === false; |
| 42 | + }, function(TextInput $input, string $arg) { |
| 43 | + return "String “{$input->getValue()}” contains a letter “{$arg}”, which is not allowed"; |
| 44 | + }, 'a') |
| 45 | + ->setDefaultValue('banana'); |
| 46 | + |
| 47 | +$form->addText('functionWithoutArg', 'Label:') |
| 48 | + ->setRequired() |
| 49 | + ->addRule(function(TextInput $input) { |
| 50 | + return strpos($input->getValue(), 'e') === false; |
| 51 | + }, function(TextInput $input) { |
| 52 | + return "String “{$input->getValue()}” contains a letter “e”, which is not allowed"; |
| 53 | + }) |
| 54 | + ->setDefaultValue('orange'); |
| 55 | + |
37 | 56 | $form->validate();
|
38 | 57 |
|
39 | 58 | Assert::true($form->hasErrors());
|
40 | 59 |
|
41 |
| -Assert::same(['1 5', '5 1', '1 ', 'Label xyz is invalid [field special] xyz'], $form->getErrors()); |
| 60 | +Assert::same([ |
| 61 | + '1 5', |
| 62 | + '5 1', |
| 63 | + '1 ', |
| 64 | + 'Label xyz is invalid [field special] xyz', |
| 65 | + 'String “banana” contains a letter “a”, which is not allowed', |
| 66 | + 'String “orange” contains a letter “e”, which is not allowed', |
| 67 | +], $form->getErrors()); |
42 | 68 |
|
43 | 69 | Assert::same([], $form->getOwnErrors());
|
44 | 70 |
|
|
0 commit comments