diff --git a/system/Language/en/Validation.php b/system/Language/en/Validation.php index ac047df280fe..3b15241b350f 100644 --- a/system/Language/en/Validation.php +++ b/system/Language/en/Validation.php @@ -24,9 +24,10 @@ // Rule Messages 'alpha' => 'The {field} field may only contain alphabetical characters.', - 'alpha_dash' => 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.', - 'alpha_numeric' => 'The {field} field may only contain alpha-numeric characters.', - 'alpha_numeric_space' => 'The {field} field may only contain alpha-numeric characters and spaces.', + 'alpha_dash' => 'The {field} field may only contain alphanumeric, underscore, and dash characters.', + 'alpha_numeric' => 'The {field} field may only contain alphanumeric characters.', + 'alpha_numeric_punct' => 'The {field} field may contain only alphanumeric characters, spaces, and ~ ! # $ % & * - _ + = | : . characters.', + 'alpha_numeric_space' => 'The {field} field may only contain alphanumeric and space characters.', 'alpha_space' => 'The {field} field may only contain alphabetical characters and spaces.', 'decimal' => 'The {field} field must contain a decimal number.', 'differs' => 'The {field} field must differ from the {param} field.', diff --git a/system/Validation/FormatRules.php b/system/Validation/FormatRules.php index c8572b091b42..e99f9df83612 100644 --- a/system/Validation/FormatRules.php +++ b/system/Validation/FormatRules.php @@ -59,8 +59,6 @@ public function alpha(string $str = null): bool return ctype_alpha($str); } - //-------------------------------------------------------------------- - /** * Alpha with spaces. * @@ -78,10 +76,8 @@ public function alpha_space(string $value = null): bool return (bool) preg_match('/^[A-Z ]+$/i', $value); } - //-------------------------------------------------------------------- - /** - * Alpha-numeric with underscores and dashes + * Alphanumeric with underscores and dashes * * @param string $str * @@ -89,13 +85,27 @@ public function alpha_space(string $value = null): bool */ public function alpha_dash(string $str = null): bool { - return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); + return (bool) preg_match('/^[a-z0-9_-]+$/i', $str); } - //-------------------------------------------------------------------- + /** + * Alphanumeric, spaces, and a limited set of punctuation characters. + * Accepted punctuation characters are: ~ tilde, ! exclamation, + * # number, $ dollar, % percent, & ampersand, * asterisk, - dash, + * _ underscore, + plus, = equals, | vertical bar, : colon, . period + * ~ ! # $ % & * - _ + = | : . + * + * @param string $str + * + * @return boolean + */ + public function alpha_numeric_punct($str) + { + return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str); + } /** - * Alpha-numeric + * Alphanumeric * * @param string $str * @@ -106,10 +116,8 @@ public function alpha_numeric(string $str = null): bool return ctype_alnum($str); } - //-------------------------------------------------------------------- - /** - * Alpha-numeric w/ spaces + * Alphanumeric w/ spaces * * @param string $str * @@ -120,8 +128,6 @@ public function alpha_numeric_space(string $str = null): bool return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str); } - //-------------------------------------------------------------------- - /** * Any type of string * @@ -137,8 +143,6 @@ public function string($str = null): bool return is_string($str); } - //-------------------------------------------------------------------- - /** * Decimal number * @@ -151,8 +155,6 @@ public function decimal(string $str = null): bool return (bool) preg_match('/^[\-+]?[0-9]+(|\.[0-9]+)$/', $str); } - //-------------------------------------------------------------------- - /** * String of hexidecimal characters * @@ -165,8 +167,6 @@ public function hex(string $str = null): bool return ctype_xdigit($str); } - //-------------------------------------------------------------------- - /** * Integer * @@ -179,8 +179,6 @@ public function integer(string $str = null): bool return (bool) preg_match('/^[\-+]?[0-9]+$/', $str); } - //-------------------------------------------------------------------- - /** * Is a Natural number (0,1,2,3, etc.) * @@ -192,8 +190,6 @@ public function is_natural(string $str = null): bool return ctype_digit($str); } - //-------------------------------------------------------------------- - /** * Is a Natural number, but not a zero (1,2,3, etc.) * @@ -205,8 +201,6 @@ public function is_natural_no_zero(string $str = null): bool return ($str !== '0' && ctype_digit($str)); } - //-------------------------------------------------------------------- - /** * Numeric * @@ -219,8 +213,6 @@ public function numeric(string $str = null): bool return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str); } - //-------------------------------------------------------------------- - /** * Compares value against a regular expression pattern. * @@ -240,8 +232,6 @@ public function regex_match(string $str = null, string $pattern, array $data): b return (bool) preg_match($pattern, $str); } - //-------------------------------------------------------------------- - /** * Validates that the string is a valid timezone as per the * timezone_identifiers_list function. @@ -257,8 +247,6 @@ public function timezone(string $str = null): bool return in_array($str, timezone_identifiers_list()); } - //-------------------------------------------------------------------- - /** * Valid Base64 * @@ -273,8 +261,6 @@ public function valid_base64(string $str = null): bool return (base64_encode(base64_decode($str)) === $str); } - //-------------------------------------------------------------------- - /** * Valid JSON * @@ -288,8 +274,6 @@ public function valid_json(string $str = null): bool return json_last_error() === JSON_ERROR_NONE; } - //-------------------------------------------------------------------- - /** * Checks for a correctly formatted email address * @@ -307,8 +291,6 @@ public function valid_email(string $str = null): bool return (bool) filter_var($str, FILTER_VALIDATE_EMAIL); } - //-------------------------------------------------------------------- - /** * Validate a comma-separated list of email addresses. * @@ -338,8 +320,6 @@ public function valid_emails(string $str = null): bool return true; } - //-------------------------------------------------------------------- - /** * Validate an IP address * @@ -367,8 +347,6 @@ public function valid_ip(string $ip = null, string $which = null, array $data): return (bool) filter_var($ip, FILTER_VALIDATE_IP, $which); } - //-------------------------------------------------------------------- - /** * Checks a URL to ensure it's formed correctly. * @@ -397,8 +375,6 @@ public function valid_url(string $str = null): bool return (filter_var($str, FILTER_VALIDATE_URL) !== false); } - //-------------------------------------------------------------------- - /** * Checks for a valid date and matches a given date format * @@ -419,5 +395,4 @@ public function valid_date(string $str = null, string $format = null): bool return (bool) $date && \DateTime::getLastErrors()['warning_count'] === 0 && \DateTime::getLastErrors()['error_count'] === 0; } - //-------------------------------------------------------------------- } diff --git a/tests/system/Validation/FormatRulesTest.php b/tests/system/Validation/FormatRulesTest.php index 544fda8aaaf1..60804ba224d5 100644 --- a/tests/system/Validation/FormatRulesTest.php +++ b/tests/system/Validation/FormatRulesTest.php @@ -1,8 +1,13 @@ -assertTrue($this->validation->run($data)); } - //-------------------------------------------------------------------- - public function testRegexMatchFalse() { $data = [ @@ -70,8 +69,6 @@ public function testRegexMatchFalse() $this->assertFalse($this->validation->run($data)); } - //-------------------------------------------------------------------- - /** * @dataProvider urlProvider */ @@ -88,8 +85,6 @@ public function testValidURL(string $url = null, bool $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function urlProvider() { return [ @@ -154,8 +149,6 @@ public function urlProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider emailProviderSingle * @@ -175,8 +168,6 @@ public function testValidEmail($email, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - /** * @dataProvider emailsProvider * @@ -196,8 +187,6 @@ public function testValidEmails($email, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function emailProviderSingle() { return [ @@ -216,8 +205,6 @@ public function emailProviderSingle() ]; } - //-------------------------------------------------------------------- - public function emailsProvider() { return [ @@ -248,8 +235,6 @@ public function emailsProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider ipProvider * @@ -270,8 +255,6 @@ public function testValidIP($ip, $which, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function ipProvider() { return [ @@ -323,8 +306,6 @@ public function ipProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider stringProvider * @@ -344,8 +325,6 @@ public function testString($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function stringProvider() { return [ @@ -364,8 +343,6 @@ public function stringProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider alphaProvider * @@ -385,25 +362,23 @@ public function testAlpha($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function alphaProvider() { return [ [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ', + FormatRulesTest::ALPHABET, true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ', + FormatRulesTest::ALPHABET . ' ', false, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1', + FormatRulesTest::ALPHABET . '1', false, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*', + FormatRulesTest::ALPHABET . '*', false, ], [ @@ -413,8 +388,6 @@ public function alphaProvider() ]; } - //-------------------------------------------------------------------- - /** * Test alpha with spaces. * @@ -436,8 +409,6 @@ public function testAlphaSpace($value, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function alphaSpaceProvider() { return [ @@ -446,26 +417,24 @@ public function alphaSpaceProvider() true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ', + FormatRulesTest::ALPHABET, true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ ', + FormatRulesTest::ALPHABET . ' ', true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ1', + FormatRulesTest::ALPHABET . '1', false, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ*', + FormatRulesTest::ALPHABET . '*', false, ], ]; } - //-------------------------------------------------------------------- - /** * @dataProvider alphaNumericProvider * @@ -485,21 +454,19 @@ public function testAlphaNumeric($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function alphaNumericProvider() { return [ [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', + FormatRulesTest::ALPHANUMERIC, true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789\ ', + FormatRulesTest::ALPHANUMERIC . '\ ', false, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789_', + FormatRulesTest::ALPHANUMERIC . '_', false, ], [ @@ -509,7 +476,98 @@ public function alphaNumericProvider() ]; } - //-------------------------------------------------------------------- + /** + * @dataProvider alphaNumericPunctProvider + * + * @param $str + * @param $expected + */ + public function testAlphaNumericPunct($str, $expected) + { + $data = [ + 'foo' => $str, + ]; + + $this->validation->setRules([ + 'foo' => 'alpha_numeric_punct', + ]); + + $this->assertEquals($expected, $this->validation->run($data)); + } + + public function alphaNumericPunctProvider() + { + return [ + [ + FormatRulesTest::ALPHANUMERIC . ' ~!#$%&*-_+=|:.', + true, + ], + [ + FormatRulesTest::ALPHANUMERIC . '`', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '@', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '^', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '(', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . ')', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '\\', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '{', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '}', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '[', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . ']', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '"', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . "'", + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '<', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '>', + false, + ], + [ + FormatRulesTest::ALPHANUMERIC . '/', + false, + ], + [ + null, + false, + ], + ]; + } /** * @dataProvider alphaNumericProvider @@ -530,17 +588,15 @@ public function testAlphaNumericSpace($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function alphaNumericSpaceProvider() { return [ [ - ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', + ' ' . FormatRulesTest::ALPHANUMERIC, true, ], [ - ' abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-', + ' ' . FormatRulesTest::ALPHANUMERIC . '-', false, ], [ @@ -550,8 +606,6 @@ public function alphaNumericSpaceProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider alphaDashProvider * @@ -571,17 +625,15 @@ public function testAlphaDash($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function alphaDashProvider() { return [ [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-', + FormatRulesTest::ALPHANUMERIC . '-', true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789-\ ', + FormatRulesTest::ALPHANUMERIC . '-\ ', false, ], [ @@ -591,8 +643,6 @@ public function alphaDashProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider hexProvider * @@ -612,8 +662,6 @@ public function testHex($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function hexProvider() { return [ @@ -622,7 +670,11 @@ public function hexProvider() true, ], [ - 'abcdefghijklmnopqrstuvwxyzABCDEFGHLIJKLMNOPQRSTUVWXYZ0123456789', + FormatRulesTest::ALPHANUMERIC, + false, + ], + [ + 'asdfjkl;', false, ], [ @@ -632,8 +684,6 @@ public function hexProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider numericProvider * @@ -653,8 +703,6 @@ public function testNumeric($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function numericProvider() { return [ @@ -689,8 +737,6 @@ public function numericProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider integerProvider * @@ -710,8 +756,6 @@ public function testInteger($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function integerProvider() { return [ @@ -746,8 +790,6 @@ public function integerProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider decimalProvider * @@ -767,8 +809,6 @@ public function testDecimal($str, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function decimalProvider() { return [ @@ -803,8 +843,6 @@ public function decimalProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider naturalProvider * @@ -824,8 +862,6 @@ public function testNatural($first, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function naturalProvider() { return [ @@ -852,8 +888,6 @@ public function naturalProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider naturalZeroProvider * @@ -873,8 +907,6 @@ public function testNaturalNoZero($first, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function naturalZeroProvider() { return [ @@ -901,8 +933,6 @@ public function naturalZeroProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider base64Provider * @@ -922,8 +952,6 @@ public function testBase64($first, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function base64Provider() { return [ @@ -942,8 +970,6 @@ public function base64Provider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider jsonProvider * @@ -963,8 +989,6 @@ public function testJson($first, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function jsonProvider() { return [ @@ -1007,8 +1031,6 @@ public function jsonProvider() ]; } - //------------------------------------------------------------------- - /** * @dataProvider timezoneProvider * @@ -1028,8 +1050,6 @@ public function testTimeZone($value, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function timezoneProvider() { return [ @@ -1052,8 +1072,6 @@ public function timezoneProvider() ]; } - //-------------------------------------------------------------------- - /** * @dataProvider validDateProvider * @@ -1074,8 +1092,6 @@ public function testValidDate($str, $format, $expected) $this->assertEquals($expected, $this->validation->run($data)); } - //-------------------------------------------------------------------- - public function validDateProvider() { return [ @@ -1242,5 +1258,4 @@ public function validDateProvider() ]; } - //-------------------------------------------------------------------- } diff --git a/user_guide_src/source/helpers/text_helper.rst b/user_guide_src/source/helpers/text_helper.rst index 593e9cb06abe..7712c161d8a6 100755 --- a/user_guide_src/source/helpers/text_helper.rst +++ b/user_guide_src/source/helpers/text_helper.rst @@ -37,7 +37,7 @@ The following functions are available: specifies the length. The following choices are available: - **alpha**: A string with lower and uppercase letters only. - - **alnum**: Alpha-numeric string with lower and uppercase characters. + - **alnum**: Alphanumeric string with lower and uppercase characters. - **basic**: A random number based on ``mt_rand()`` (length ignored). - **numeric**: Numeric string. - **nozero**: Numeric string with no zeros. diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 19a468b0a3c8..a708649d3237 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -676,10 +676,15 @@ Rule Parameter Description ======================= =========== =============================================================================================== =================================================== alpha No Fails if field has anything other than alphabetic characters. alpha_space No Fails if field contains anything other than alphabetic characters or spaces. -alpha_dash No Fails if field contains anything other than alpha-numeric characters, underscores or dashes. -alpha_numeric No Fails if field contains anything other than alpha-numeric characters or numbers. -alpha_numeric_space No Fails if field contains anything other than alpha-numeric characters, numbers or space. -decimal No Fails if field contains anything other than a decimal number. +alpha_dash No Fails if field contains anything other than alphanumeric characters, underscores or dashes. +alpha_numeric No Fails if field contains anything other than alphanumeric characters. +alpha_numeric_space No Fails if field contains anything other than alphanumeric or space characters. +alpha_numeric_punct No Fails if field contains anything other than alphanumeric, space, or this limited set of + punctuation characters: ~ (tilde), ! (exclamation), # (number), $ (dollar), % (percent), + & (ampersand), * (asterisk), - (dash), _ (underscore), + (plus), = (equals), + | (vertical bar), : (colon), . (period). +decimal No Fails if field contains anything other than a decimal number. + Also accepts a + or - sign for the number. differs Yes Fails if field does not differ from the one in the parameter. differs[field_name] exact_length Yes Fails if field is not exactly the parameter value. One or more comma-separated values. exact_length[5] or exact_length[5,8,12] greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]