summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2017-01-06 11:37:30 +0100
committerAndrey Andreev <narf@devilix.net>2017-01-06 11:37:30 +0100
commitbda2efd42e8896c03d6a95591a6d77bcb761ce26 (patch)
treef8a26b0573e09cfc98a18a54234b210aef151b67
parentad29ba888068485de20980df240ae786aa53a9ab (diff)
Add valid_mac() FV rule
Close #3992
-rw-r--r--system/language/english/form_validation_lang.php1
-rw-r--r--system/libraries/Form_validation.php25
-rw-r--r--tests/codeigniter/libraries/Form_validation_test.php15
-rw-r--r--user_guide_src/source/changelog.rst1
-rw-r--r--user_guide_src/source/libraries/form_validation.rst1
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
@@ -1284,6 +1284,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}(?<delimiter>[:-])([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
*
* @param string
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 <libraries/table>` 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.
========================= ========== ============================================================================================= =======================