Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added array support & bug fixes.

Specific elements in array are found through dot notation.
Array support: Some credits to blackbelt devs.

Signed-off-by: Tasos Bekos <tbekos@gmail.com>
  • Loading branch information...
commit c364975b2daa228d0d9f576ef6518ddb2bda1694 1 parent 272c76d
@bekos authored
Showing with 69 additions and 24 deletions.
  1. +63 −15 BaseValidator.php
  2. +3 −6 Validator.php
  3. +3 −3 ext/Validator_EL.php
View
78 BaseValidator.php
@@ -7,6 +7,7 @@
* Extend to provide specific rules and error messages.
*
* @author Tasos Bekos <tbekos@gmail.com>
+ * @see https://github.com/bekos/php-validation
* @see Based on idea: http://brettic.us/2010/06/18/form-validation-class-using-php-5-3/
* @abstract
*/
@@ -106,7 +107,7 @@ function __construct($data = null) {
if (is_null($data)) {
$data = 'POST'; // No need to duplicate POST variables
}
- $this->data = $data;
+ $this->data = (is_string($data)) ? strtoupper($data) : $data;
// Error messages
$this->errorMessages = $this->buildErrorMessages();
@@ -212,14 +213,21 @@ public function getAllErrors($keys = TRUE) {
* @return mixed
*/
protected function getVal($key) {
+ $pos = strpos($key, '.'); // Whether we have array with dot key notation
+ $index = FALSE;
+ if ($pos !== FALSE) {
+ $index = substr($key, $pos + 1);
+ $key = substr($key, 0, $pos);
+ }
+
if (is_string($this->data)) {
switch ($this->data) {
case 'POST':
- return (isset($_POST[$key])) ? $_POST[$key] : FALSE;
+ $value = (isset($_POST[$key])) ? $_POST[$key] : FALSE;
break;
case 'GET':
- return (isset($_GET[$key])) ? $_GET[$key] : FALSE;
+ $value = (isset($_GET[$key])) ? $_GET[$key] : FALSE;
break;
default:
@@ -227,7 +235,39 @@ protected function getVal($key) {
break;
}
} else {
- return (isset($this->data[$key])) ? $this->data[$key] : FALSE;
+ $value = (isset($this->data[$key])) ? $this->data[$key] : FALSE;
+ }
+
+ if ($index !== FALSE && $value !== FALSE && is_array($value)) {
+ // Get value in multidimensional array with dot key notation
+ $value = self::_getVal(explode('.', $index), $value);
+ }
+
+ return $value;
+ }
+
+ /**
+ * Navigate through a [multidimensional] array looking for a particular index.
+ *
+ * @param array $index The index sequence we are navigating down.
+ * @param array $value The portion of the array to process.
+ * @return mixed
+ */
+ private static function _getVal($index, $value) {
+ if (is_array($index) && count($index)) {
+ $currentIndex = array_shift($index);
+ }
+
+ if (isset($currentIndex) && isset($value[$currentIndex])) {
+ $value = $value[$currentIndex];
+ } else {
+ return FALSE;
+ }
+
+ if (is_array($value) && count($value)) {
+ return self::_getVal($index, $value);
+ } else {
+ return $value;
}
}
@@ -314,15 +354,25 @@ public function validate($key, $label = '') {
* @return boolean TRUE if no error found, FALSE otherwise.
*/
private function _validate($val, $key) {
- // try each rule function
- foreach ($this->rules as $rule) {
- $ruleId = $rule['id'];
- $function = $this->functions[$ruleId];
-
- $valid = (isset($rule['args'])) ? $function($val, $rule['args']) : $function($val);
- if ($valid === FALSE) {
- $this->registerError($rule, $key);
- return FALSE;
+ if (is_array($val)) {
+ // Run validations on every element of array.
+ // If one of them fails, return FALSE.
+ foreach ($val as $_val) {
+ if ($this->_validate($_val, $key) === FALSE) {
+ return FALSE;
+ }
+ }
+ } else {
+ // Try each rule function
+ foreach ($this->rules as $rule) {
+ $ruleId = $rule['id'];
+ $function = $this->functions[$ruleId];
+
+ $valid = (isset($rule['args'])) ? $function($val, $rule['args']) : $function($val);
+ if ($valid === FALSE) {
+ $this->registerError($rule, $key);
+ return FALSE;
+ }
}
}
@@ -341,8 +391,6 @@ private function registerError($rule, $key) {
} else {
$ruleId = $rule['id'];
-
-
$args = (isset($rule['args'])) ? $rule['args'] : null;
$message = $this->getErrorMessage($ruleId, $args);
}
View
9 Validator.php
@@ -5,6 +5,7 @@
/**
* Validation rules.
*
+ * @see https://github.com/bekos/php-validation
* @author Tasos Bekos <tbekos@gmail.com>
*/
class Validator extends BaseValidator {
@@ -105,7 +106,7 @@ protected function buildErrorMessages() {
*/
public function email($message = null) {
$this->setRule(__FUNCTION__, function($email) {
- return (strlen($email) === 0 || filter_var($email, FILTER_VALIDATE_EMAIL) === TRUE);
+ return (strlen($email) === 0 || filter_var($email, FILTER_VALIDATE_EMAIL) !== FALSE);
}, $message);
return $this;
}
@@ -118,11 +119,7 @@ public function email($message = null) {
*/
public function required($message = null) {
$this->setRule(__FUNCTION__, function($string) {
- if (is_array($string)) {
- return!(empty($string));
- } else {
- return (strlen(trim($string)) > 0);
- }
+ return (strlen(trim($string)) > 0);
}, $message);
return $this;
}
View
6 ext/Validator_EL.php
@@ -32,10 +32,10 @@ protected function buildErrorMessages() {
'integer' => '%s πρέπει να είναι ακέραιος.',
'positive' => '%s πρέπει να είναι θετικός αριθμός.',
'negative' => '%s πρέπει να είναι αρνητικός αριθμός.',
- 'lowercase' => '%s πρέπει να περίεχει μόνο πεζά γράμματα.',
- 'uppercase' => '%s πρέπει να περίεχει μόνο κεφαλαία γράμματα.',
+ 'lowercase' => '%s πρέπει να περιέχει μόνο πεζά γράμματα.',
+ 'uppercase' => '%s πρέπει να περιέχει μόνο κεφαλαία γράμματα.',
'float' => '%s πρέπει να είναι έγκυρος αριθμός.',
- 'digits' => '%s πρέπει να περίεχει αριθμούς.',
+ 'digits' => '%s πρέπει να περιέχει αριθμούς.',
'min' => function($args) {
$message = '%s πρέπει να έχει τιμή μεγαλύτερη ';
if ($args[1] == TRUE) {
Please sign in to comment.
Something went wrong with that request. Please try again.