Summary of FuelPHP standard validation rules

table of contents
Hello.
I'm Mandai, the Wild Team member of the development team.
This time, I'd like to share as much information as possible about the standard FuelPHP validation rules, including things you should know and unexpected uses, as well as information that isn't included in the documentation.
The version of FuelPHP used in this post is 1.7.2.
required
| Rule Name | argument |
|---|---|
| required | none |
Required checks for required fields and passes if the value is anything other than "false", "null", "empty string", or "empty array with no elements".
Note that validation rules other than required can pass with an empty string.
required_with
| Rule Name | argument |
|---|---|
| required_with | $fieldname |
The required rule will be applied only if the data in the field named $fieldname passes the required rule. For example,
you could use it to validate the contents of a text box if a checkbox is checked.
match_value
| Rule Name | argument |
|---|---|
| match_value | $compare, $strict = false |
Compares whether the data in $compare is the same.
For strings and numbers, $compare compares the type using "===".
If $strict is false, the comparison ignores the type using "==".
If $compare is an array, it scans the array and compares it in order, including the type using "===".
If there are identical values, the validation passes.
If $strict is false, it ignores the type using "==".
match_pattern
| Rule Name | argument |
|---|---|
| match_pattern | $pattern |
The preg_match function performs pattern matching using regular expressions.
Mass-produce input forms quickly, elegantly, and wildly with Fuelphp's fieldset | Beyond Inc. also has a sample, so please take a look.
match_field
| Rule Name | argument |
|---|---|
| match_field | $field |
performs a comparison including type checking with the field specified in $field.
Since strict cannot be specified for this rule, it will only pass if it is "===" or an empty string.
match_collection
| Rule Name | argument |
|---|---|
| match_collection | $collection = array(), $strict = false |
If $collection is an array, the in_array function compares it with the values in the array.
If $strict is true, the third argument to the in_array function is set to true, and a strict comparison including type checking is performed.
If $collection is not an array, all given arguments are set to an array and a comparison is performed using the in_array function.
For example, if the data "1, true, 2, 'test3'" is given as an argument, even if you enter 1 in the form, it will not pass because $strict is set to true (because numbers sent via POST are treated as strings).
In effect, the only string that can pass this validation is test3.
Also, if the data "1, 'true', 2, 'test3'" is used as an argument, it will appear that $strict is set to true, but since it is not a Boolean value, the comparison will be performed without type checking.
Therefore, when validating the data sent via POST, only "true" and "test3" will pass.
The specifications are unclear and difficult to understand, and because it behaves the same as when the the match_value rule is an array, entering data other than an array into $collection and using it could be an unexpected pitfall.
min_length
| Rule Name | argument |
|---|---|
| min_length | $length |
If the string size is $length or more, it will pass.
If the size is 0 (= empty string), it will also pass, so even if you set $length = 1, it will not be a required field validation.
max_length
| Rule Name | argument |
|---|---|
| max_length | $length |
If the string size is less than or equal to $length, it will pass
exact_length
| Rule Name | argument |
|---|---|
| exact_length | $length |
If the string size is $length, it can be passed.
It can also be passed if the size is 0 (= empty string), so it may be often used in combination with required.
valid_date
| Rule Name | argument |
|---|---|
| valid_date | $format = null, $strict = true |
This function verifies whether the given string is data that conforms to the date and time format.
It goes without saying that an empty string can be passed, but if you only look at the documentation, you may not realize that there is no need to specify $format.
If a format is set in $format, parsing will be performed based on $format by the date_parse_from_format function.
If $format is null, parsing will be performed by the date_parse function, so there is no need to specify a format (although data that can be parsed as a date may be passed by mistake).
The advantage of setting $format to null is that it allows the rule to pass both "Ymd" and "Y/m/d"
valid_email
| Rule Name | argument |
|---|---|
| valid_email | none |
Use the filter_var function to verify whether the string is a valid email address.
Validating an email address with the filter_var function is much more secure than using your own regular expression with the preg_match function.
valid_emails
| Rule Name | argument |
|---|---|
| valid_emails | $separator (optional) |
After using explode to decompose the input data using the string set in $separator, the valid_email rule is validated in a loop
The default value for $separator is ","
valid_url
| Rule Name | argument |
|---|---|
| valid_url | none |
The filter_var function is used to verify whether the string is valid as a URL.
One pitfall of the filter_var function is that it cannot pass URLs that contain multibyte characters, even if they are real URLs.
valid_ip
| Rule Name | argument |
|---|---|
| valid_ip | none |
Use the filter_var function to verify whether the string is a valid IP address.
This can be used to verify both IPv4 and IPv6.
numeric_min
| Rule Name | argument |
|---|---|
| numeric_min | $min_val |
Validates whether the input value is greater than or equal to $min_val as a number.
Internally, the input value and $min_val are cast to float and compared, so decimals can also be compared.
numeric_max
| Rule Name | argument |
|---|---|
| numeric_max | $max_val |
Validates whether the input value is less than or equal to $max_val as a number.
Internally, the input value and $max_val are cast to float and compared, so decimals can also be compared.
numeric_between
| Rule Name | argument |
|---|---|
| numeric_between | $min_val, $max_val |
Validates whether the input value is greater than or equal to $min_val and less than or equal to $max_val.
This validates both the numeric_min and numeric_max rules at the same time.
valid_string
| Rule Name | argument |
|---|---|
| valid_string | $flags = array('alpha', 'utf8') |
Just select a rule to build a general-purpose regular expression and perform verification.
You can do the same thing with
match_pattern The strings that can be stored in the array stored in $flags are as follows.
| Flags | Target character |
|---|---|
| alpha | Uppercase and lowercase alphabets |
| numeric | Numbers 0-9 |
| specials | Alphabetic (strings pattern matched with [:alpha:]) |
| spaces | Half-width space |
| newlines | \r \n |
| tabs | \t |
| punctuation | . , ! ? : ; & |
| singlequotes | ' |
| doublequotes | " |
| dashes | _ - |
| forward slashes | / |
| backslashes | \ |
| brackets | [ ] |
| braces | ( ) |
| utf8 | { } |
Internally, these rules are combined to create a regular expression.
You need to specify several of them in an array, but if the input data format is fixed, there is a hidden function that automatically sets rules that match the data format.
For URLs, simply setting "$flags = 'url_safe'" will set three flags: 'alpha', 'numeric', and 'dashes'.
These rules (or rule sets, which are sets of multiple rules?) actually exist as a hidden feature.
| Ruleset Name | Rules to be set |
|---|---|
| alpha | alpha, utf8 |
| alpha_numeric | alpha, utf8, numeric |
| specials | specials, utf8 |
| url_safe | alpha, numeric, dashes |
| integer, numeric | numeric |
| float | numeric, dots |
| quotes | single quotes, double quotes |
| slashes | forward slashes, backslashes |
| all | alpha, utf8, numeric, specials, spaces, newlines, tabs, punctuation, singlequotes, doublequotes, dashes, forwardslashes, backslashes, brackets, braces |
The desperation of "all" is incredible (when will I ever use this)
That's all
1