diff --git a/system/Language/en/Validation.php b/system/Language/en/Validation.php index fb43b66c67d7..d918daefb629 100644 --- a/system/Language/en/Validation.php +++ b/system/Language/en/Validation.php @@ -30,6 +30,7 @@ '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.', + 'equals' => 'The {field} field must be exactly: {param}.', 'exact_length' => 'The {field} field must be exactly {param} characters in length.', 'greater_than' => 'The {field} field must contain a number greater than {param}.', 'greater_than_equal_to' => 'The {field} field must contain a number greater than or equal to {param}.', @@ -43,6 +44,7 @@ 'matches' => 'The {field} field does not match the {param} field.', 'max_length' => 'The {field} field cannot exceed {param} characters in length.', 'min_length' => 'The {field} field must be at least {param} characters in length.', + 'not_equals' => 'The {field} field cannot be: {param}.', 'numeric' => 'The {field} field must contain only numbers.', 'regex_match' => 'The {field} field is not in the correct format.', 'required' => 'The {field} field is required.', diff --git a/system/Validation/Rules.php b/system/Validation/Rules.php index bc37610b4be1..32c0a579bc71 100644 --- a/system/Validation/Rules.php +++ b/system/Validation/Rules.php @@ -65,6 +65,21 @@ public function differs(string $str = null, string $field, array $data): bool //-------------------------------------------------------------------- + /** + * Equals the static value provided. + * + * @param string $str + * @param string $val + * + * @return boolean + */ + public function equals(string $str = null, string $val): bool + { + return $str === $val; + } + + //-------------------------------------------------------------------- + /** * Returns true if $str is $val characters long. * $val = "5" (one) | "5,8,12" (multiple values) @@ -261,6 +276,21 @@ public function min_length(string $str = null, string $val, array $data): bool //-------------------------------------------------------------------- + /** + * Does not equal the static value provided. + * + * @param string $str + * @param string $val + * + * @return boolean + */ + public function not_equals(string $str = null, string $val): bool + { + return $str !== $val; + } + + //-------------------------------------------------------------------- + /** * Required * diff --git a/tests/system/Validation/RulesTest.php b/tests/system/Validation/RulesTest.php index 3c9f17da1ca2..af9aebac8484 100644 --- a/tests/system/Validation/RulesTest.php +++ b/tests/system/Validation/RulesTest.php @@ -379,6 +379,157 @@ public function testDiffersFalse() $this->assertFalse($this->validation->run($data)); } + + //-------------------------------------------------------------------- + + public function testEqualsNull() + { + $data = [ + 'foo' => null, + ]; + + $this->validation->setRules([ + 'foo' => 'equals[]', + ]); + + $this->assertFalse($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testEqualsEmptyIsEmpty() + { + $data = [ + 'foo' => '', + ]; + + $this->validation->setRules([ + 'foo' => 'equals[]', + ]); + + $this->assertTrue($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testEqualsReturnsFalseOnFailure() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'equals[notbar]', + ]); + + $this->assertFalse($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testEqualsReturnsTrueOnSuccess() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'equals[bar]', + ]); + + $this->assertTrue($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testEqualsReturnsFalseOnCaseMismatch() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'equals[Bar]', + ]); + + $this->assertFalse($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testNotEqualsNull() + { + $data = [ + 'foo' => null, + ]; + + $this->validation->setRules([ + 'foo' => 'not_equals[]', + ]); + + $this->assertTrue($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testNotEqualsEmptyIsEmpty() + { + $data = [ + 'foo' => '', + ]; + + $this->validation->setRules([ + 'foo' => 'not_equals[]', + ]); + + $this->assertFalse($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testNotEqualsReturnsFalseOnFailure() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'not_equals[bar]', + ]); + + $this->assertFalse($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testNotEqualsReturnsTrueOnSuccess() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'not_equals[notbar]', + ]); + + $this->assertTrue($this->validation->run($data)); + } + + //-------------------------------------------------------------------- + + public function testNotEqualsReturnsTrueOnCaseMismatch() + { + $data = [ + 'foo' => 'bar', + ]; + + $this->validation->setRules([ + 'foo' => 'not_equals[Bar]', + ]); + + $this->assertTrue($this->validation->run($data)); + } + //-------------------------------------------------------------------- diff --git a/user_guide_src/source/libraries/validation.rst b/user_guide_src/source/libraries/validation.rst index 83e2e4fc1d62..d3987946da7c 100644 --- a/user_guide_src/source/libraries/validation.rst +++ b/user_guide_src/source/libraries/validation.rst @@ -661,7 +661,8 @@ alpha_numeric No Fails if field contains anything other than 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. 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] +equals Yes Fails if field is not exactly the parameter value. +exact_length Yes Fails if field is not exactly the parameter value in length. 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] greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5] in_list Yes Fails if field is not within a predetermined list. in_list[red,blue,green] @@ -673,6 +674,7 @@ less_then_equal_to Yes Fails if field is greater than the parameter matches Yes The value must match the value of the field in the parameter. matches[field] max_length Yes Fails if field is longer than the parameter value. max_length[8] min_length Yes Fails if field is shorter than the parameter value. min_length[3] +not_equals Yes Fails if field is exactly the parameter value. numeric No Fails if field contains anything other than numeric characters. regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/] if_exist No If this rule is present, validation will only return possible errors if the field key exists,