Merge pull request #25 from neves/fix-addRule

Fix add rule
This commit is contained in:
Vance Lucas 2014-01-11 19:48:40 -08:00
commit e011ab81c4
3 changed files with 20 additions and 1 deletions

View File

@ -49,7 +49,8 @@ class Validator
// Load language file in directory // Load language file in directory
$langFile = rtrim($langDir, '/') . '/' . $lang . '.php'; $langFile = rtrim($langDir, '/') . '/' . $lang . '.php';
if ( stream_resolve_include_path($langFile) ) { if ( stream_resolve_include_path($langFile) ) {
static::$_ruleMessages = include $langFile; $langMessages = include $langFile;
static::$_ruleMessages = array_merge(static::$_ruleMessages, $langMessages);
} else { } else {
throw new InvalidArgumentException("fail to load language file '$langFile'"); throw new InvalidArgumentException("fail to load language file '$langFile'");
} }

View File

@ -11,6 +11,7 @@ class BaseTestCase extends \PHPUnit_Framework_TestCase
{ {
$this->resetProperty('_lang'); $this->resetProperty('_lang');
$this->resetProperty('_langDir'); $this->resetProperty('_langDir');
$this->resetProperty('_rules', array());
$this->resetProperty('_ruleMessages', array()); $this->resetProperty('_ruleMessages', array());
} }

View File

@ -10,4 +10,21 @@ class StaticVsInstanceTest extends BaseTestCase
$this->assertEquals('ar', Validator::lang(), $this->assertEquals('ar', Validator::lang(),
'instance defined lang should not replace static global 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]);
}
} }