How to use CakePHP Validation class
We can use CakePHP validation functions and speed up development. These functions already follow the correct rules so we don’t need to write custom code.
Example:
In Controller:
$validate = new Validation();
$string = ‘Name123’;
$result = $validate->alphaNumeric ( $string );
if ($result) {
echo ‘Success’;
} else {
echo ‘Fail’;
}
exit ();
Default functions with syntax:
1. notEmpty($check): Checks that a string contains something other than whitespace.
2. alphaNumeric($check): Checks that a string contains only integer or letters.
3. between($check, $min, $max): Checks that a string length is within specified range.
4. blank($check): Returns true if field is left blank -OR- only whitespace characters are present in its value.
5. cc($check, $type = ‘fast’, $deep = false, $regex = null): Validation of credit card numbers.
6. comparison($check1, $operator = null, $check2 = null): Used to compare 2 numeric values.
7. custom($check, $regex = null): Used when a custom regular expression is needed.
8. date($check, $format = ‘ymd’, $regex = null): Date validation, determines if the string passed is a valid date.
9. datetime($check, $dateFormat = ‘ymd’, $regex = null): Validates a datetime value.
10. time($check): Time validation, determines if the string passed is a valid time.
11. decimal($check, $places = null, $regex = null): Checks that a value is a valid decimal. Both the sign and exponent are optional.
12. email($check, $deep = false, $regex = null): Validates for an email address.
13. extension($check, $extensions = array(‘gif’, ‘jpeg’, ‘png’, ‘jpg’)): Check that value has a valid file extension.
14. ip($check, $type = ‘both’): Validation of an IP address.
15. money($check, $symbolPosition = ‘left’): Checks that a value is a monetary amount.
16. numeric($check): Checks if a value is numeric.
17. naturalNumber($check, $allowZero = false): Checks if a value is a natural number.
18. phone($check, $regex = null, $country = ‘all’): Check that a value is a valid phone number.
19. postal($check, $regex = null, $country = ‘us’): Checks that a given value is a valid postal code.
20. ssn($check, $regex = null, $country = null): Checks that a value is a valid Social Security Number.
21. url($check, $strict = false): Checks that a value is a valid URL like https://webnersolutions.com/text/URL/file-path.txt
22. inList($check, $list, $caseInsensitive = false): Checks if a value is in a given list. Comparison is case sensitive by default.