Skip to content

Commit

Permalink
Merge pull request #1952 from MGatner/validation-equals-notequals
Browse files Browse the repository at this point in the history
Add Validations for `equals()` and `not_equals()`
  • Loading branch information
lonnieezell authored Apr 19, 2019
2 parents ebd203a + 1f2c731 commit 9bdfb13
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
2 changes: 2 additions & 0 deletions system/Language/en/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}.',
Expand All @@ -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.',
Expand Down
30 changes: 30 additions & 0 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
*
Expand Down
151 changes: 151 additions & 0 deletions tests/system/Validation/RulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}


//--------------------------------------------------------------------

Expand Down
4 changes: 3 additions & 1 deletion user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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,
Expand Down

0 comments on commit 9bdfb13

Please sign in to comment.