mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Error messages now include field name by default
* Version 1.1.0 release
This commit is contained in:
parent
13853d226c
commit
05c1b9ab15
@ -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"],
|
||||
|
||||
48
lang/en.php
48
lang/en.php
@ -1,29 +1,29 @@
|
||||
<?php
|
||||
return array(
|
||||
'required' => "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"
|
||||
);
|
||||
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
61
tests/Valitron/ErrorMessages.php
Normal file
61
tests/Valitron/ErrorMessages.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
use Valitron\Validator;
|
||||
|
||||
class ErrorMessagesTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testErrorMessageIncludesFieldName()
|
||||
{
|
||||
$v = new Validator(array());
|
||||
$v->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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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'));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user