diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index d92e2e6..cfb2da1 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -49,7 +49,8 @@ class Validator // Load language file in directory $langFile = rtrim($langDir, '/') . '/' . $lang . '.php'; if ( stream_resolve_include_path($langFile) ) { - static::$_ruleMessages = include $langFile; + $langMessages = include $langFile; + static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages); } else { throw new InvalidArgumentException("fail to load language file '$langFile'"); } diff --git a/tests/Valitron/BaseTestCase.php b/tests/Valitron/BaseTestCase.php index 0dec6c4..9754f01 100644 --- a/tests/Valitron/BaseTestCase.php +++ b/tests/Valitron/BaseTestCase.php @@ -11,6 +11,7 @@ class BaseTestCase extends \PHPUnit_Framework_TestCase { $this->resetProperty('_lang'); $this->resetProperty('_langDir'); + $this->resetProperty('_rules', array()); $this->resetProperty('_ruleMessages', array()); } diff --git a/tests/Valitron/StaticVsInstanceTest.php b/tests/Valitron/StaticVsInstanceTest.php index c83dad4..1601189 100644 --- a/tests/Valitron/StaticVsInstanceTest.php +++ b/tests/Valitron/StaticVsInstanceTest.php @@ -10,4 +10,21 @@ class StaticVsInstanceTest extends BaseTestCase $this->assertEquals('ar', Validator::lang(), 'instance defined lang should not replace static global lang'); } + + /** + * Fix bug where rules messages added with Validator::addRule were replaced after creating validator instance + */ + public function testRuleMessagesReplacedAfterConstructor() + { + $customMessage = 'custom message'; + $ruleName = 'customRule'; + $fieldName = 'fieldName'; + Validator::addRule($ruleName, function() {}, $customMessage); + $v = new Validator(array($fieldName => $fieldName)); + $v->rule($ruleName, $fieldName); + $v->validate(); + $messages = $v->errors(); + $this->assertArrayHasKey($fieldName, $messages); + $this->assertEquals(ucfirst("$fieldName $customMessage"), $messages[$fieldName][0]); + } }