From bda2efd42e8896c03d6a95591a6d77bcb761ce26 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Fri, 6 Jan 2017 12:37:30 +0200 Subject: Add valid_mac() FV rule Close #3992 --- system/language/english/form_validation_lang.php | 1 + system/libraries/Form_validation.php | 25 ++++++++++++++++++++++ .../codeigniter/libraries/Form_validation_test.php | 15 +++++++++++-- user_guide_src/source/changelog.rst | 1 + .../source/libraries/form_validation.rst | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/system/language/english/form_validation_lang.php b/system/language/english/form_validation_lang.php index aa9ff330b..357f747ce 100644 --- a/system/language/english/form_validation_lang.php +++ b/system/language/english/form_validation_lang.php @@ -43,6 +43,7 @@ $lang['form_validation_valid_email'] = 'The {field} field must contain a valid $lang['form_validation_valid_emails'] = 'The {field} field must contain all valid email addresses.'; $lang['form_validation_valid_url'] = 'The {field} field must contain a valid URL.'; $lang['form_validation_valid_ip'] = 'The {field} field must contain a valid IP.'; +$lang['form_validation_valid_mac'] = 'The {field} field must contain a valid MAC.'; $lang['form_validation_min_length'] = 'The {field} field must be at least {param} characters in length.'; $lang['form_validation_max_length'] = 'The {field} field cannot exceed {param} characters in length.'; $lang['form_validation_exact_length'] = 'The {field} field must be exactly {param} characters in length.'; diff --git a/system/libraries/Form_validation.php b/system/libraries/Form_validation.php index 6f654deb4..11f98f2a9 100644 --- a/system/libraries/Form_validation.php +++ b/system/libraries/Form_validation.php @@ -1283,6 +1283,31 @@ class CI_Form_validation { // -------------------------------------------------------------------- + /** + * Validate MAC address + * + * @param string $mac + * @return bool + */ + public function valid_mac($mac) + { + if ( ! is_php('5.5')) + { + // Most common format, with either dash or colon delimiters + if (preg_match('#\A[0-9a-f]{2}(?[:-])([0-9a-f]{2}((?P=delimiter)|\z)){5}#i', $mac)) + { + return TRUE; + } + + // The less common format; e.g. 0123.4567.89ab + return (bool) preg_match('#((\A|\.)[0-9a-f]{4}){3}\z#i', $mac); + } + + return (bool) filter_var($mac, FILTER_VALIDATE_MAC); + } + + // -------------------------------------------------------------------- + /** * Alpha * diff --git a/tests/codeigniter/libraries/Form_validation_test.php b/tests/codeigniter/libraries/Form_validation_test.php index edbe9da4a..035410724 100644 --- a/tests/codeigniter/libraries/Form_validation_test.php +++ b/tests/codeigniter/libraries/Form_validation_test.php @@ -268,7 +268,6 @@ class Form_validation_test extends CI_TestCase { public function test_rule_valid_email() { $this->assertTrue($this->form_validation->valid_email('email@sample.com')); - $this->assertFalse($this->form_validation->valid_email('valid_email', '@sample.com')); } @@ -294,10 +293,22 @@ class Form_validation_test extends CI_TestCase { $this->assertFalse($this->form_validation->valid_ip('127.0.0.259')); } + public function test_rule_valid_mac() + { + $this->assertTrue($this->form_validation->valid_mac("01-23-45-67-89-aB")); + $this->assertTrue($this->form_validation->valid_mac("01:23:45:67:89:aB")); + $this->assertTrue($this->form_validation->valid_mac("0123.4567.89aB")); + + $this->assertFalse($this->form_validation->valid_mac("-01-23-45-67-89-ab")); + $this->assertFalse($this->form_validation->valid_mac("01:23:45:67:89:ab:")); + $this->assertFalse($this->form_validation->valid_mac("01:23:45:67:89:ab\n")); + $this->assertFalse($this->form_validation->valid_mac("01:23:45:67:89:ag:")); + $this->assertFalse($this->form_validation->valid_mac('0123456789ab')); + } + public function test_rule_valid_base64() { $this->assertTrue($this->form_validation->valid_base64(base64_encode('string'))); - $this->assertFalse($this->form_validation->valid_base64('FA08GG')); } diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index a8467dcf1..e83ad41ad 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -45,6 +45,7 @@ Release Date: Not Released - Removed previously deprecated method ``prep_for_form()`` / rule *prep_for_form*. - Changed method ``set_rules()`` to throw a ``BadMethodCallException`` when its first parameter is not an array and the ``$rules`` one is unused. + - Added rule **valid_mac**, which replicates PHP's native ``filter_var()`` with ``FILTER_VALIDATE_MAC``. - :doc:`HTML Table Library ` changes include: diff --git a/user_guide_src/source/libraries/form_validation.rst b/user_guide_src/source/libraries/form_validation.rst index 915ce876f..65fd9acc8 100644 --- a/user_guide_src/source/libraries/form_validation.rst +++ b/user_guide_src/source/libraries/form_validation.rst @@ -987,6 +987,7 @@ Rule Parameter Description **valid_emails** No Returns FALSE if any value provided in a comma separated list is not a valid email. **valid_ip** No Returns FALSE if the supplied IP is not valid. Accepts an optional parameter of 'ipv4' or 'ipv6' to specify an IP format. +**valid_mac** No Returns FALSE if the supplied MAC address is not valid. **valid_base64** No Returns FALSE if the supplied string contains anything other than valid Base64 characters. ========================= ========== ============================================================================================= ======================= -- cgit v1.2.3-24-g4f1b