The "numeric" validator is simply a shim to call PHP's is_numeric() function,
but some might be surprised at the range of values that PHP considers to be
numeric:
3.14
1.0e-1
-5
Some developers might have assumed that "numeric" meant non-negative integers
only -- particularly considering that that (along with allowing alphabetic
characters) is the behavior of the not-so-differently-named "alphaNumeric"
validator.
Even the author of bake apparently thought "numeric" meant non-negative
integers; bake's suggested validation for all my id fields is "numeric", but I
would not consider "3.14", "1.0e-1" or "-5" to be valid ids, and I would want
my model's validation to know that.
Integer validation (positive, negative and zero) should be as simple as
preg_match('/^-?\d+$/', $value). (PHP offers an is_int() function, but it
requires the type of the variable to be an integer, whereas with data that
comes from web forms we're often talking about strings representing integers.)
Non-negative integer validation would be preg_match('/^\d+$/', $value). (If you
have the ctype functions available, PHP offers ctype_digit(), but it requires
the type of the variable to be a string, and fails if it is an actual integer
variable.)
So, why doesn't CakePHP have built-in integer and digit validation functions
and suggest these for use as default validators for integer and unsigned
integer fields, respectively?
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php