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",
|
"name": "vlucas/valitron",
|
||||||
"version": "1.0.4",
|
"version": "1.1.0",
|
||||||
"type": "library",
|
"type": "library",
|
||||||
"description": "Simple, elegant, stand-alone validation library with NO dependencies",
|
"description": "Simple, elegant, stand-alone validation library with NO dependencies",
|
||||||
"keywords": ["validation", "validator", "valid"],
|
"keywords": ["validation", "validator", "valid"],
|
||||||
|
|||||||
48
lang/en.php
48
lang/en.php
@ -1,29 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
return array(
|
return array(
|
||||||
'required' => "Required",
|
'required' => "is required",
|
||||||
'equals' => "Must be the same as '%s'",
|
'equals' => "must be the same as '%s'",
|
||||||
'different' => "Must be different than '%s'",
|
'different' => "must be different than '%s'",
|
||||||
'accepted' => "Must be accepted",
|
'accepted' => "must be accepted",
|
||||||
'numeric' => "Must be numeric",
|
'numeric' => "must be numeric",
|
||||||
'integer' => "Must be an integer (0-9)",
|
'integer' => "must be an integer (0-9)",
|
||||||
'length' => "Must be longer than %d",
|
'length' => "must be longer than %d",
|
||||||
'min' => "Must be greater than %s",
|
'min' => "must be greater than %s",
|
||||||
'max' => "Must be less than %s",
|
'max' => "must be less than %s",
|
||||||
'in' => "Invalid value",
|
'in' => "contains invalid value",
|
||||||
'notIn' => "Invalid value",
|
'notIn' => "contains invalid value",
|
||||||
'ip' => "Invalid IP address",
|
'ip' => "is not a valid IP address",
|
||||||
'email' => "Invalid email address",
|
'email' => "is not a valid email address",
|
||||||
'url' => "Invalid URL",
|
'url' => "not a URL",
|
||||||
'urlActive' => "Must be active domain",
|
'urlActive' => "must be an active domain",
|
||||||
'alpha' => "Must contain only letters a-z",
|
'alpha' => "must contain only letters a-z",
|
||||||
'alphaNum' => "Must contain only letters a-z and/or numbers 0-9",
|
'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",
|
'slug' => "must contain only letters a-z, numbers 0-9, dashes and underscores",
|
||||||
'regex' => "Invalid format",
|
'regex' => "contains invalid chatacters",
|
||||||
'date' => "Invalid date",
|
'date' => "is not a valid date",
|
||||||
'dateFormat' => "Must be date with format '%s'",
|
'dateFormat' => "must be date with format '%s'",
|
||||||
'dateBefore' => "Must be date before '%s'",
|
'dateBefore' => "must be date before '%s'",
|
||||||
'dateAfter' => "Must be date after '%s'",
|
'dateAfter' => "must be date after '%s'",
|
||||||
'contains' => "Must contain %s"
|
'contains' => "must contain %s"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -464,6 +464,7 @@ class Validator
|
|||||||
}
|
}
|
||||||
$values[] = $param;
|
$values[] = $param;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_errors[$field][] = vsprintf($msg, $values);
|
$this->_errors[$field][] = vsprintf($msg, $values);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -567,7 +568,7 @@ class Validator
|
|||||||
'rule' => $rule,
|
'rule' => $rule,
|
||||||
'fields' => (array) $fields,
|
'fields' => (array) $fields,
|
||||||
'params' => (array) $params,
|
'params' => (array) $params,
|
||||||
'message' => $message
|
'message' => '{field} ' . $message
|
||||||
);
|
);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
@ -612,6 +613,8 @@ class Validator
|
|||||||
$i++;
|
$i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
$msg = str_replace('{field}', ucwords(str_replace('_', ' ', $field)), $msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $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');
|
$v->rule('required', 'name');
|
||||||
$this->assertSame(1, count($v->errors('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()
|
public function testArrayOfFieldsToValidate()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user