diff --git a/src/ValidatorChain.php b/src/ValidatorChain.php index d6262d6b5..d6ca6cbcb 100644 --- a/src/ValidatorChain.php +++ b/src/ValidatorChain.php @@ -177,7 +177,7 @@ public function isValid($value, $context = null) } $result = false; $messages = $validator->getMessages(); - $this->messages = array_merge($this->messages, $messages); + $this->messages = array_replace_recursive($this->messages, $messages); if ($element['breakChainOnFailure']) { break; } diff --git a/src/ValidatorPluginManager.php b/src/ValidatorPluginManager.php index b84e6c196..5d7770084 100644 --- a/src/ValidatorPluginManager.php +++ b/src/ValidatorPluginManager.php @@ -107,6 +107,13 @@ class ValidatorPluginManager extends AbstractPluginManager 'step' => 'Zend\Validator\Step', ); + /** + * Whether or not to share by default; default to false + * + * @var bool + */ + protected $shareByDefault = false; + /** * Constructor * diff --git a/test/ValidatorChainTest.php b/test/ValidatorChainTest.php index 932bbe44e..ba1084e54 100644 --- a/test/ValidatorChainTest.php +++ b/test/ValidatorChainTest.php @@ -39,9 +39,15 @@ class ValidatorChainTest extends \PHPUnit_Framework_TestCase public function setUp() { + AbstractValidator::setMessageLength(-1); $this->validator = new ValidatorChain(); } + public function tearDown() + { + AbstractValidator::setMessageLength(-1); + } + public function populateValidatorChain() { $this->validator->addValidator(new NotEmpty()); @@ -220,4 +226,51 @@ public function getValidatorFalse() ->will($this->returnValue(array('error' => 'validation failed'))); return $validator; } + + /** + * @group ZF-412 + */ + public function testCanAttachMultipleValidatorsOfTheSameTypeAsDiscreteInstances() + { + $this->validator->addByName('Callback', array( + 'callback' => function ($value) { + return true; + }, + 'messages' => array( + 'callbackValue' => 'This should not be seen in the messages', + ), + )); + $this->validator->addByName('Callback', array( + 'callback' => function ($value) { + return false; + }, + 'messages' => array( + 'callbackValue' => 'Second callback trapped', + ), + )); + + $this->assertEquals(2, count($this->validator)); + $validators = $this->validator->getValidators(); + $compare = null; + foreach ($validators as $validator) { + $this->assertNotSame($compare, $validator); + $compare = $validator; + } + + $this->assertFalse($this->validator->isValid('foo')); + $messages = $this->validator->getMessages(); + $found = false; + $test = 'Second callback trapped'; + foreach ($messages as $messageSet) { + if (is_string($messageSet) && $messageSet === $test) { + $found = true; + break; + } + if (is_array($messageSet) && in_array('Second callback trapped', $messageSet)) { + $found = true; + break; + } + } + $this->assertTrue($found); + } }