-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathRules.formatMessage.phpt
More file actions
79 lines (58 loc) · 2.07 KB
/
Rules.formatMessage.phpt
File metadata and controls
79 lines (58 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Test: Nette\Forms\Rules::formatMessage()
*/
declare(strict_types=1);
use Nette\Forms\Controls\TextInput;
use Nette\Forms\Form;
use Tester\Assert;
require __DIR__ . '/../bootstrap.php';
$form = new Form;
$form->addText('args1')
->setRequired()
->addRule(Form::RANGE, '%d %d', [1, 5])
->setDefaultValue('x');
$form->addText('args2')
->setRequired()
->addRule(Form::RANGE, '%2$d %1$d', [1, 5])
->setDefaultValue('x');
$form->addText('args3')
->setRequired()
->addRule(Form::LENGTH, '%d %d', 1)
->setDefaultValue('xyz');
$form->addText('special', 'Label:')
->setRequired()
->addRule(Form::EMAIL, '%label %value is invalid [field %name] %d', $form['special'])
->setDefaultValue('xyz');
$form->addText('function', 'Label:')
->setRequired()
->addRule(function (TextInput $input, string $arg) {
return strpos($input->getValue(), $arg) === false;
}, function (TextInput $input, string $arg) {
return "String “{$input->getValue()}” contains a letter “{$arg}”, which is not allowed";
}, 'a')
->setDefaultValue('banana');
$form->addText('functionWithoutArg', 'Label:')
->setRequired()
->addRule(function (TextInput $input) {
return strpos($input->getValue(), 'e') === false;
}, function (TextInput $input) {
return "String “{$input->getValue()}” contains a letter “e”, which is not allowed";
})
->setDefaultValue('orange');
$form->validate();
Assert::true($form->hasErrors());
Assert::same([
'1 5',
'5 1',
'1 ',
'Label xyz is invalid [field special] xyz',
'String “banana” contains a letter “a”, which is not allowed',
'String “orange” contains a letter “e”, which is not allowed',
], $form->getErrors());
Assert::same([], $form->getOwnErrors());
Assert::same(['1 5'], $form['args1']->getErrors());
Assert::same(['5 1'], $form['args2']->getErrors());
Assert::same(['1 '], $form['args3']->getErrors());
Assert::same(['Label xyz is invalid [field special] xyz'], $form['special']->getErrors());
Assert::match('%A%data-nette-rules=\'%A%{"op":":email","msg":"Label %value is invalid [field special] %0"%A%', $form->__toString(true));