Skip to content

Commit 46fe9a4

Browse files
committed
Merge branch 'feature/add-message'
2 parents 2910202 + bc4a780 commit 46fe9a4

File tree

5 files changed

+79
-3
lines changed

5 files changed

+79
-3
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 1.1.0 - 2018-08-28
6+
7+
### Added
8+
9+
- [#75](https://github.com/xtreamwayz/html-form-validator/pull/75) adds ability to add custom messages after validation.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- Nothing.
26+
527
## 1.0.1 - 2018-06-12
628

729
### Added

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
"require-dev": {
2626
"phpunit/phpunit": "^7.0",
2727
"squizlabs/php_codesniffer": "^3.0",
28-
"phpstan/phpstan": "^0.9.2"
28+
"phpstan/phpstan": "^0.10"
2929
},
3030
"suggest": {
3131
"zendframework/zend-servicemanager": "To support third-party validators and filters"
3232
},
3333
"extra": {
3434
"branch-alias": {
35-
"dev-master": "1.0.x-dev",
36-
"dev-develop": "2.0.x-dev"
35+
"dev-master": "1.2.x-dev"
3736
},
3837
"zf": {
3938
"config-provider": "Xtreamwayz\\HTMLFormValidator\\ConfigProvider"

src/ValidationResult.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Xtreamwayz\HTMLFormValidator;
66

7+
use function array_replace_recursive;
78
use function count;
89

910
final class ValidationResult implements ValidationResultInterface
@@ -61,6 +62,14 @@ public function getClicked() : ?string
6162
return $this->submitName;
6263
}
6364

65+
/**
66+
* @inheritdoc
67+
*/
68+
public function addMessages(array $messages) : void
69+
{
70+
$this->messages = array_replace_recursive($this->messages, $messages);
71+
}
72+
6473
/**
6574
* @inheritdoc
6675
*/

src/ValidationResultInterface.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ public function isClicked(string $name) : bool;
2424
*/
2525
public function getClicked() : ?string;
2626

27+
/**
28+
* Add custom validation messages
29+
*
30+
* Must have the same format as zend-validator messages:
31+
*
32+
* [
33+
* '<field>' => [
34+
* '<error_code>' => '<message>',
35+
* ],
36+
* 'email' => [
37+
* 'emailAddressInvalidFormat' => 'The given email address is invalid',
38+
* ],
39+
* ]
40+
*
41+
*/
42+
public function addMessages(array $messages) : void;
43+
2744
/**
2845
* Get validation messages
2946
*/

test/ValidationResultTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace XtreamwayzTest\HTMLFormValidator;
66

77
use PHPUnit\Framework\TestCase;
8+
use function var_dump;
89
use Xtreamwayz\HTMLFormValidator\ValidationResult;
910

1011
class ValidationResultTest extends TestCase
@@ -87,4 +88,32 @@ public function testSubmitButtonIsNotClicked() : void
8788
self::assertFalse($result->isClicked('cancel'));
8889
self::assertNull($result->getClicked());
8990
}
91+
92+
public function testMessagesCanBeAdded() : void
93+
{
94+
$result = new ValidationResult($this->rawValues, $this->values, $this->messages, 'POST');
95+
96+
$result->addMessages([
97+
'foo' => [
98+
'invalidUuid' => 'This is not a valid uuid',
99+
'notInArray' => 'This is not in array',
100+
],
101+
'baz' => [
102+
'isRequired' => 'This is required',
103+
],
104+
]);
105+
106+
$expected = [
107+
'foo' => [
108+
'regexNotMatch' => '',
109+
'invalidUuid' => 'This is not a valid uuid',
110+
'notInArray' => 'This is not in array',
111+
],
112+
'baz' => [
113+
'isRequired' => 'This is required',
114+
],
115+
];
116+
117+
self::assertEquals($expected, $result->getMessages());
118+
}
90119
}

0 commit comments

Comments
 (0)