diff --git a/README.md b/README.md index f52c4f6..4e58837 100644 --- a/README.md +++ b/README.md @@ -168,6 +168,37 @@ $v->rule('min', 'bar', 0); $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 The test suite depends on the Composer autoloader to load and run the diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c9e55e3..42cdc01 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -4,7 +4,7 @@ namespace Valitron; /** * Validation Class * - * Validates input against certain criteria + * Validates input against certian criteria * * @package Valitron * @author Vance Lucas @@ -15,6 +15,7 @@ class Validator protected $_fields = array(); protected $_errors = array(); protected $_validations = array(); + protected $_labels = array(); protected static $_lang; protected static $_langDir; @@ -32,16 +33,16 @@ class Validator { // Allows filtering of used input fields against optional second array of field names allowed // This is useful for limiting raw $_POST or $_GET data to only known fields - foreach ($data as $field => $value) { - if (empty($fields) || (!empty($fields) && in_array($field, $fields))) { + foreach($data as $field => $value) { + if(empty($fields) || (!empty($fields) && in_array($field, $fields))) { $this->_fields[$field] = $value; } } // Only load language files if language or directory has changed - if ($lang !== static::$_lang || $langDir !== static::$_langDir) { + if($lang !== static::$_lang || $langDir !== static::$_langDir) { // Set language directory for loading language files - if ($langDir === null) { + if($langDir === null) { $langDir = dirname(dirname(__DIR__)) . '/lang'; } static::langDir($langDir); @@ -56,7 +57,7 @@ class Validator */ public static function lang($lang = null) { - if ($lang !== null) { + if($lang !== null) { static::$_lang = $lang; // Load language file in directory @@ -71,7 +72,7 @@ class Validator */ public static function langDir($dir = null) { - if ($dir !== null) { + if($dir !== null) { static::$_langDir = $dir; } return static::$_langDir; @@ -82,9 +83,9 @@ class Validator */ protected function validateRequired($field, $value) { - if (is_null($value)) { + if(is_null($value)) { return false; - } elseif (is_string($value) and trim($value) === '') { + } elseif(is_string($value) and trim($value) === '') { return false; } return true; @@ -95,7 +96,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return void */ protected function validateEquals($field, $value, array $params) @@ -109,7 +110,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateDifferent($field, $value, array $params) @@ -162,14 +163,14 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateLength($field, $value, $params) { $length = $this->stringLength($value); // Length between - if (isset($params[1])) { + if(isset($params[1])) { return $length >= $params[0] && $length <= $params[1]; } // Length same @@ -195,7 +196,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateMin($field, $value, $params) @@ -208,7 +209,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateMax($field, $value, $params) @@ -221,7 +222,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateIn($field, $value, $params) @@ -234,7 +235,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateNotIn($field, $value, $params) @@ -252,7 +253,7 @@ class Validator */ protected function validateContains($field, $value, $params) { - if (!isset($params[0])) { + if(!isset($params[0])) { return false; } if (!is_string($params[0]) || !is_string($value)) { @@ -295,7 +296,7 @@ class Validator protected function validateUrl($field, $value) { foreach ($this->validUrlPrefixes as $prefix) { - if (strpos($value, $prefix) !== false) { + if(strpos($value, $prefix) !== false) { return filter_var($value, FILTER_VALIDATE_URL) !== false; } } @@ -312,7 +313,7 @@ class Validator protected function validateUrlActive($field, $value) { foreach ($this->validUrlPrefixes as $prefix) { - if (strpos($value, $prefix) !== false) { + if(strpos($value, $prefix) !== false) { $url = str_replace($prefix, '', strtolower($value)); return checkdnsrr($url); @@ -362,7 +363,6 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params * @return bool */ protected function validateRegex($field, $value, $params) @@ -387,7 +387,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateDateFormat($field, $value, $params) @@ -402,7 +402,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateDateBefore($field, $value, $params) @@ -417,7 +417,7 @@ class Validator * * @param string $field * @param mixed $value - * @param array $params + * @param array $fields * @return bool */ protected function validateDateAfter($field, $value, $params) @@ -440,7 +440,7 @@ class Validator */ public function errors($field = null) { - if ($field !== null) { + if($field !== null) { return isset($this->_errors[$field]) ? $this->_errors[$field] : false; } return $this->_errors; @@ -451,13 +451,15 @@ class Validator */ public function error($field, $msg, array $params = array()) { + $msg = $this->checkAndSetLabel($field, $msg, $params); + $values = array(); // Printed values need to be in string format - foreach ($params as $param) { - if (is_array($param)) { + foreach($params as $param) { + if(is_array($param)) { $param = "['" . implode("', '", $param) . "']"; } - if ($param instanceof \DateTime) { + if($param instanceof \DateTime) { $param = $param->format('Y-m-d'); } $values[] = $param; @@ -482,6 +484,7 @@ class Validator $this->_fields = array(); $this->_errors = array(); $this->_validations = array(); + $this->_labels = array(); } /** @@ -491,12 +494,12 @@ class Validator */ public function validate() { - foreach ($this->_validations as $v) { - foreach ($v['fields'] as $field) { + foreach($this->_validations as $v) { + foreach($v['fields'] as $field) { $value = isset($this->_fields[$field]) ? $this->_fields[$field] : null; // Callback is user-specified or assumed method on class - if (isset(static::$_rules[$v['rule']])) { + if(isset(static::$_rules[$v['rule']])) { $callback = static::$_rules[$v['rule']]; } else { $callback = array($this, 'validate' . ucfirst($v['rule'])); @@ -516,7 +519,7 @@ class Validator */ public static function addRule($name, $callback, $message = self::ERROR_DEFAULT) { - if (!is_callable($callback)) { + if(!is_callable($callback)) { throw new \InvalidArgumentException("Second argument must be a valid callback. Given argument was not callable."); } @@ -529,9 +532,9 @@ class Validator */ public function rule($rule, $fields) { - if (!isset(static::$_rules[$rule])) { + if(!isset(static::$_rules[$rule])) { $ruleMethod = 'validate' . ucfirst($rule); - if (!method_exists($this, $ruleMethod)) { + if(!method_exists($this, $ruleMethod)) { throw new \InvalidArgumentException("Rule '" . $rule . "' has not been registered with " . __CLASS__ . "::addRule()."); } } @@ -551,6 +554,51 @@ class Validator 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 */ diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index cf66088..2f0bda6 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -47,6 +47,36 @@ class ValidateTest extends \PHPUnit_Framework_TestCase $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() { $v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));