Guest User

Untitled

a guest
Feb 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. <?php
  2. require 'vendor/autoload.php';
  3. use Respect\Validation\Validator as v;
  4. $errors = [];
  5. class Validation {
  6. public $errors = [];
  7. /**
  8. * validating from input
  9. * @param array $array it will be $_POST or $_GET
  10. * @param array $rules rules for validations
  11. * @return array errors
  12. */
  13. public function validate($array, $rules)
  14. {
  15. $errors = [];
  16. foreach ($rules as $key => $value)
  17. {
  18. // check whether rules key available inside $_POST array
  19. if (!array_key_exists($key, $array)) {
  20. $errors = [];
  21. $errors['not found'] = $key . ' is not submitted';
  22. return $errors;
  23. }
  24. $subrules = explode('|', $value);
  25. foreach ($subrules as $subrule) {
  26. $terms = explode(':', $subrule);
  27. switch ($terms[0]) {
  28. case "min":
  29. $min = $terms[1];
  30. !v::alpha()->min($min)->validate($array[$key]) && $errors[$key] = "$key can't be less than $min characters";
  31. break;
  32. case "max":
  33. $max = $terms[1];
  34. !v::alpha()->max($max)->validate($array[$key]) && $errors[$key] = "$key can't be more than $max characters";
  35. break;
  36. case "email":
  37. !v::email()->validate($array[$key]) && $errors[$key] = "$key should be a valid email address";
  38. break;
  39. case "same":
  40. !v::equals($array[$terms[1]])->validate($array[$key]) && $errors[$key] = "$key should be same as $terms[1]";
  41. default:
  42. break;
  43. }
  44. }
  45. }
  46. return $errors;
  47. }
  48. }
Add Comment
Please, Sign In to add comment