Skip to content
This repository was archived by the owner on Jan 30, 2020. It is now read-only.

Commit e04114e

Browse files
committed
Merge branch 'hotfix/73'
Close #73
2 parents c907c3a + 8b6c26d commit e04114e

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ All notable changes to this project will be documented in this file, in reverse
2020

2121
### Fixed
2222

23-
- [#67](https://github.com/zendframework/zend-inputfilter/pull/67) fixes
23+
- [#67](https://github.com/zendframework/zend-inputfilter/pull/67) and
24+
[#73](https://github.com/zendframework/zend-inputfilter/pull/73) fix
2425
localization of the `NotEmpty` validation error message (created for any
2526
required input for which a value was not provided).
2627

src/Input.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,16 @@ protected function injectNotEmptyValidator()
491491
*/
492492
protected function prepareRequiredValidationFailureMessage()
493493
{
494-
$notEmpty = $this->getValidatorChain()->plugin(NotEmpty::class);
494+
$chain = $this->getValidatorChain();
495+
$notEmpty = $chain->plugin(NotEmpty::class);
496+
497+
foreach ($chain->getValidators() as $validator) {
498+
if ($validator['instance'] instanceof NotEmpty) {
499+
$notEmpty = $validator['instance'];
500+
break;
501+
}
502+
}
503+
495504
$templates = $notEmpty->getOption('messageTemplates');
496505
$message = $templates[NotEmpty::IS_EMPTY];
497506
$translator = $notEmpty->getTranslator();

test/InputTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,39 @@ public function testRequiredWithoutFallbackAndValueNotSetProvidesNotEmptyValidat
227227
$this->assertRequiredValidationErrorMessage($input);
228228
}
229229

230+
/**
231+
* @group 28
232+
* @group 69
233+
*/
234+
public function testRequiredWithoutFallbackAndValueNotSetProvidesAttachedNotEmptyValidatorIsEmptyErrorMessage()
235+
{
236+
$input = new Input();
237+
$input->setRequired(true);
238+
239+
$customMessage = [
240+
NotEmptyValidator::IS_EMPTY => "Custom message",
241+
];
242+
243+
$notEmpty = $this->getMockBuilder(NotEmptyValidator::class)
244+
->setMethods(['getOption'])
245+
->getMock();
246+
247+
$notEmpty->expects($this->once())
248+
->method('getOption')
249+
->with('messageTemplates')
250+
->willReturn($customMessage);
251+
252+
$input->getValidatorChain()
253+
->attach($notEmpty);
254+
255+
$this->assertFalse(
256+
$input->isValid(),
257+
'isValid() should always return false when no fallback value is present, '
258+
. 'the input is required, and no data is set.'
259+
);
260+
$this->assertEquals($customMessage, $input->getMessages());
261+
}
262+
230263
/**
231264
* @group 28
232265
* @group 60

0 commit comments

Comments
 (0)