diff --git a/composer.json b/composer.json index 0f55eee..96f2c3b 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "vlucas/valitron", - "version": "1.0.4", + "version": "1.1.0", "type": "library", "description": "Simple, elegant, stand-alone validation library with NO dependencies", "keywords": ["validation", "validator", "valid"], diff --git a/lang/en.php b/lang/en.php index ab718b4..36c3358 100644 --- a/lang/en.php +++ b/lang/en.php @@ -1,29 +1,29 @@ "Required", - 'equals' => "Must be the same as '%s'", - 'different' => "Must be different than '%s'", - 'accepted' => "Must be accepted", - 'numeric' => "Must be numeric", - 'integer' => "Must be an integer (0-9)", - 'length' => "Must be longer than %d", - 'min' => "Must be greater than %s", - 'max' => "Must be less than %s", - 'in' => "Invalid value", - 'notIn' => "Invalid value", - 'ip' => "Invalid IP address", - 'email' => "Invalid email address", - 'url' => "Invalid URL", - 'urlActive' => "Must be active domain", - 'alpha' => "Must contain only letters a-z", - 'alphaNum' => "Must contain only letters a-z and/or numbers 0-9", - 'slug' => "Must contain only letters a-z, numbers 0-9, dashes and underscores", - 'regex' => "Invalid format", - 'date' => "Invalid date", - 'dateFormat' => "Must be date with format '%s'", - 'dateBefore' => "Must be date before '%s'", - 'dateAfter' => "Must be date after '%s'", - 'contains' => "Must contain %s" + 'required' => "is required", + 'equals' => "must be the same as '%s'", + 'different' => "must be different than '%s'", + 'accepted' => "must be accepted", + 'numeric' => "must be numeric", + 'integer' => "must be an integer (0-9)", + 'length' => "must be longer than %d", + 'min' => "must be greater than %s", + 'max' => "must be less than %s", + 'in' => "contains invalid value", + 'notIn' => "contains invalid value", + 'ip' => "is not a valid IP address", + 'email' => "is not a valid email address", + 'url' => "not a URL", + 'urlActive' => "must be an active domain", + 'alpha' => "must contain only letters a-z", + 'alphaNum' => "must contain only letters a-z and/or numbers 0-9", + 'slug' => "must contain only letters a-z, numbers 0-9, dashes and underscores", + 'regex' => "contains invalid chatacters", + 'date' => "is not a valid date", + 'dateFormat' => "must be date with format '%s'", + 'dateBefore' => "must be date before '%s'", + 'dateAfter' => "must be date after '%s'", + 'contains' => "must contain %s" ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 6daf22a..d5736c8 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -464,6 +464,7 @@ class Validator } $values[] = $param; } + $this->_errors[$field][] = vsprintf($msg, $values); } @@ -567,7 +568,7 @@ class Validator 'rule' => $rule, 'fields' => (array) $fields, 'params' => (array) $params, - 'message' => $message + 'message' => '{field} ' . $message ); return $this; } @@ -612,6 +613,8 @@ class Validator $i++; } } + } else { + $msg = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $msg); } return $msg; diff --git a/tests/Valitron/ErrorMessages.php b/tests/Valitron/ErrorMessages.php new file mode 100644 index 0000000..b517e2f --- /dev/null +++ b/tests/Valitron/ErrorMessages.php @@ -0,0 +1,61 @@ +rule('required', 'name'); + $v->validate(); + $this->assertSame(array("Name is required"), $v->errors('name')); + } + + public function testAccurateErrorMessageParams() + { + $v = new Validator(array('num' => 5)); + $v->rule('min', 'num', 6); + $v->validate(); + $this->assertSame(array("Num must be greater than 6"), $v->errors('num')); + } + + public function testCustomErrorMessage() + { + $v = new Validator(array()); + $v->rule('required', 'name')->message('Name is required'); + $v->validate(); + $errors = $v->errors('name'); + $this->assertSame('Name is required', $errors[0]); + } + + public function testCustomLabel() + { + $v = new Validator(array()); + $v->rule('required', 'name')->message('{field} is required')->label('Custom Name'); + $v->validate(); + $errors = $v->errors('name'); + $this->assertSame('Custom Name is required', $errors[0]); + } + + public function testCustomLabels() + { + $messages = array( + 'name' => array('Name is required'), + 'email' => array('Email should be a valid email address') + ); + + $v = new Validator(array('name' => '', 'email' => '$')); + $v->rule('required', 'name')->message('{field} is required'); + $v->rule('email', 'email')->message('{field} should be a valid email address'); + + $v->labels(array( + 'name' => 'Name', + 'email' => 'Email' + )); + + $v->validate(); + $errors = $v->errors(); + $this->assertEquals($messages, $errors); + } +} + diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index d1ba60d..6516007 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -21,62 +21,6 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $v->rule('required', 'name'); $this->assertSame(1, count($v->errors('name'))); } - - public function testAccurateErrorMessage() - { - $v = new Validator(array()); - $v->rule('required', 'name'); - $v->validate(); - $this->assertSame(array("Required"), $v->errors('name')); - } - - public function testAccurateErrorMessageParams() - { - $v = new Validator(array('num' => 5)); - $v->rule('min', 'num', 6); - $v->validate(); - $this->assertSame(array("Must be greater than 6"), $v->errors('num')); - } - - public function testCustomErrorMessage() - { - $v = new Validator(array()); - $v->rule('required', 'name')->message('Name is required'); - $v->validate(); - $errors = $v->errors('name'); - $this->assertSame('Name is required', $errors[0]); - } - - public function testCustomLabel() - { - $v = new Validator(array()); - $v->rule('required', 'name')->message('{field} is required')->label('Name'); - $v->validate(); - $errors = $v->errors('name'); - $this->assertSame('Name is required', $errors[0]); - } - - public function testCustomLabels() - { - $messages = array( - 'name' => array('Name is required'), - 'email' => array('Email should be a valid email address') - ); - - $v = new Validator(array('name' => '', 'email' => '$')); - $v->rule('required', 'name')->message('{field} is required'); - $v->rule('email', 'email')->message('{field} should be a valid email address'); - - $v->labels(array( - 'name' => 'Name', - 'email' => 'Email' - )); - - $v->validate(); - $errors = $v->errors(); - $this->assertEquals($messages, $errors); - } - public function testArrayOfFieldsToValidate() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));