Added in field labels to allow user to label fields with a different name than the actual label defined in the html.

This commit is contained in:
Andrew Smith 2013-05-13 13:16:22 +01:00
parent 285c56ba74
commit 4a14b0e13e
3 changed files with 143 additions and 34 deletions

View File

@ -168,6 +168,37 @@ $v->rule('min', 'bar', 0);
$v->validate(); $v->validate();
``` ```
## Adding field label to messages
You can do this in two different ways, you can add a individual label to a rule or an array of all labels for the rules.
To add individual label to rule you simply add the `label` method after the rule.
```php
$v = new Valitron\Validator(array());
$v->rule('required', 'name')->message('{field} is required')->label('Name');
$v->validate();
There is a edge case to this method, you wouldn't be able to use a array of field names in the rule definition, so one rule per field. So this wouldn't work:
```php
$v = new Valitron\Validator(array());
$v->rule('required', array('name', 'email'))->message('{field} is required')->label('Name');
$v->validate();
However we can use a array of labels to solve this issue by simply adding the `labels` method instead:
```php
$v = new Valitron\Validator(array());
$v->rule('required', array('name', 'email'))->message('{field} is required');
$v->rules(array(
'name' => 'Name',
'email' => 'Email address'
));
$v->validate();
This introduces a new set of tags to your error language file which looks like `{field}`, if you are using a rule like `equals` you can access the second value in the language file by incrementing the field with a value like `{field1}`.
## Running Tests ## Running Tests
The test suite depends on the Composer autoloader to load and run the The test suite depends on the Composer autoloader to load and run the

View File

@ -4,7 +4,7 @@ namespace Valitron;
/** /**
* Validation Class * Validation Class
* *
* Validates input against certain criteria * Validates input against certian criteria
* *
* @package Valitron * @package Valitron
* @author Vance Lucas <vance@vancelucas.com> * @author Vance Lucas <vance@vancelucas.com>
@ -15,6 +15,7 @@ class Validator
protected $_fields = array(); protected $_fields = array();
protected $_errors = array(); protected $_errors = array();
protected $_validations = array(); protected $_validations = array();
protected $_labels = array();
protected static $_lang; protected static $_lang;
protected static $_langDir; protected static $_langDir;
@ -95,7 +96,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return void * @return void
*/ */
protected function validateEquals($field, $value, array $params) protected function validateEquals($field, $value, array $params)
@ -109,7 +110,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateDifferent($field, $value, array $params) protected function validateDifferent($field, $value, array $params)
@ -162,7 +163,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateLength($field, $value, $params) protected function validateLength($field, $value, $params)
@ -195,7 +196,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateMin($field, $value, $params) protected function validateMin($field, $value, $params)
@ -208,7 +209,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateMax($field, $value, $params) protected function validateMax($field, $value, $params)
@ -221,7 +222,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateIn($field, $value, $params) protected function validateIn($field, $value, $params)
@ -234,7 +235,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateNotIn($field, $value, $params) protected function validateNotIn($field, $value, $params)
@ -362,7 +363,6 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params
* @return bool * @return bool
*/ */
protected function validateRegex($field, $value, $params) protected function validateRegex($field, $value, $params)
@ -387,7 +387,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateDateFormat($field, $value, $params) protected function validateDateFormat($field, $value, $params)
@ -402,7 +402,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateDateBefore($field, $value, $params) protected function validateDateBefore($field, $value, $params)
@ -417,7 +417,7 @@ class Validator
* *
* @param string $field * @param string $field
* @param mixed $value * @param mixed $value
* @param array $params * @param array $fields
* @return bool * @return bool
*/ */
protected function validateDateAfter($field, $value, $params) protected function validateDateAfter($field, $value, $params)
@ -451,6 +451,8 @@ class Validator
*/ */
public function error($field, $msg, array $params = array()) public function error($field, $msg, array $params = array())
{ {
$msg = $this->checkAndSetLabel($field, $msg, $params);
$values = array(); $values = array();
// Printed values need to be in string format // Printed values need to be in string format
foreach($params as $param) { foreach($params as $param) {
@ -482,6 +484,7 @@ class Validator
$this->_fields = array(); $this->_fields = array();
$this->_errors = array(); $this->_errors = array();
$this->_validations = array(); $this->_validations = array();
$this->_labels = array();
} }
/** /**
@ -551,6 +554,51 @@ class Validator
return $this; return $this;
} }
/**
* @param array $labels
* @return $this
*/
public function label($value)
{
$lastRules = $this->_validations[count($this->_validations)-1]['fields'];
$this->labels(array($lastRules[0] => $value));
return $this;
}
/**
* @param array $labels
* @return $this
*/
public function labels($labels = array())
{
$this->_labels = array_merge($this->_labels, $labels);
return $this;
}
/**
* @param $field
* @return array
*/
private function checkAndSetLabel($field, $msg, $params)
{
if (isset($this->_labels[$field])) {
$msg = str_replace('{field}', $this->_labels[$field], $msg);
if (is_array($params)) {
$i = 1;
foreach ($params as $k => $v) {
$tag = '{field'. $i .'}';
$label = isset($params[$k]) && isset($this->_labels[$params[$k]]) ? $this->_labels[$params[$k]] : $tag;
$msg = str_replace($tag, $label, $msg);
$i++;
}
}
}
return $msg;
}
/** /**
* Convenience method to add multiple validation rules with an array * Convenience method to add multiple validation rules with an array
*/ */

View File

@ -47,6 +47,36 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
$this->assertSame('Name is required', $errors[0]); $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());
$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'));