Advertisement
tilz0R

Valitron optional rule

Nov 30th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.42 KB | None | 0 0
  1. use Valitron\Validator as Validate;
  2.  
  3. //Test 1, valid input
  4. $v = new Validate(['name' => 'myemail@gmail.com']);
  5. //We are optional, not needed to be included in data array
  6. $v->rule('optional', 'name')
  7. //But if we are included, then we have to be valid email address
  8.   ->rule('email', 'name')->message('Wrong EMAIL format');
  9.  
  10. //Print result
  11. print "Result 1";
  12. var_dump($v->validate());
  13. var_dump($v->errors());
  14.  
  15. //Test 2, invalid input, it is not email and it is not minlength 10 characters
  16. $v = new Validate(['name' => '']);
  17. //We are optional, not needed to be included in data array
  18. $v->rule('optional', 'name')
  19. //But if we are included, then we have to be valid email address
  20.   ->rule('email', 'name')->message('Wrong EMAIL format')
  21. //And email length must be at least 10 characters
  22.   ->rule('lengthMin', 'name', 10)->message('Too small');
  23.  
  24. //Print result
  25. print "Result 2";
  26. var_dump($v->validate());
  27. var_dump($v->errors());
  28.  
  29. //Test 2, valid input, "name" does not exists in data array
  30. $v = new Validate([]);
  31. //We are optional, not needed to be included in data array
  32. $v->rule('optional', 'name')
  33. //But if we are included, then we have to be valid email address
  34.   ->rule('email', 'name')->message('Wrong EMAIL format')
  35. //And email length must be at least 10 characters
  36.   ->rule('lengthMin', 'name', 10)->message('Too small');
  37.  
  38. //Print result
  39. print "Result 3";
  40. var_dump($v->validate());
  41. var_dump($v->errors());
  42.  
  43. exit;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement