Skip to content

Commit

Permalink
Merge branch 'feature/add-message'
Browse files Browse the repository at this point in the history
  • Loading branch information
geerteltink committed Aug 28, 2018
2 parents 2910202 + bc4a780 commit 46fe9a4
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

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

## 1.1.0 - 2018-08-28

### Added

- [#75](https://github.com/xtreamwayz/html-form-validator/pull/75) adds ability to add custom messages after validation.

### Changed

- Nothing.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 1.0.1 - 2018-06-12

### Added
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@
"require-dev": {
"phpunit/phpunit": "^7.0",
"squizlabs/php_codesniffer": "^3.0",
"phpstan/phpstan": "^0.9.2"
"phpstan/phpstan": "^0.10"
},
"suggest": {
"zendframework/zend-servicemanager": "To support third-party validators and filters"
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev",
"dev-develop": "2.0.x-dev"
"dev-master": "1.2.x-dev"
},
"zf": {
"config-provider": "Xtreamwayz\\HTMLFormValidator\\ConfigProvider"
Expand Down
9 changes: 9 additions & 0 deletions src/ValidationResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Xtreamwayz\HTMLFormValidator;

use function array_replace_recursive;
use function count;

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

/**
* @inheritdoc
*/
public function addMessages(array $messages) : void
{
$this->messages = array_replace_recursive($this->messages, $messages);
}

/**
* @inheritdoc
*/
Expand Down
17 changes: 17 additions & 0 deletions src/ValidationResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ public function isClicked(string $name) : bool;
*/
public function getClicked() : ?string;

/**
* Add custom validation messages
*
* Must have the same format as zend-validator messages:
*
* [
* '<field>' => [
* '<error_code>' => '<message>',
* ],
* 'email' => [
* 'emailAddressInvalidFormat' => 'The given email address is invalid',
* ],
* ]
*
*/
public function addMessages(array $messages) : void;

/**
* Get validation messages
*/
Expand Down
29 changes: 29 additions & 0 deletions test/ValidationResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace XtreamwayzTest\HTMLFormValidator;

use PHPUnit\Framework\TestCase;
use function var_dump;
use Xtreamwayz\HTMLFormValidator\ValidationResult;

class ValidationResultTest extends TestCase
Expand Down Expand Up @@ -87,4 +88,32 @@ public function testSubmitButtonIsNotClicked() : void
self::assertFalse($result->isClicked('cancel'));
self::assertNull($result->getClicked());
}

public function testMessagesCanBeAdded() : void
{
$result = new ValidationResult($this->rawValues, $this->values, $this->messages, 'POST');

$result->addMessages([
'foo' => [
'invalidUuid' => 'This is not a valid uuid',
'notInArray' => 'This is not in array',
],
'baz' => [
'isRequired' => 'This is required',
],
]);

$expected = [
'foo' => [
'regexNotMatch' => '',
'invalidUuid' => 'This is not a valid uuid',
'notInArray' => 'This is not in array',
],
'baz' => [
'isRequired' => 'This is required',
],
];

self::assertEquals($expected, $result->getMessages());
}
}

0 comments on commit 46fe9a4

Please sign in to comment.