Skip to content

Commit 9bdfb13

Browse files
authored
Merge pull request #1952 from MGatner/validation-equals-notequals
Add Validations for `equals()` and `not_equals()`
2 parents ebd203a + 1f2c731 commit 9bdfb13

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

system/Language/en/Validation.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
'alpha_space' => 'The {field} field may only contain alphabetical characters and spaces.',
3131
'decimal' => 'The {field} field must contain a decimal number.',
3232
'differs' => 'The {field} field must differ from the {param} field.',
33+
'equals' => 'The {field} field must be exactly: {param}.',
3334
'exact_length' => 'The {field} field must be exactly {param} characters in length.',
3435
'greater_than' => 'The {field} field must contain a number greater than {param}.',
3536
'greater_than_equal_to' => 'The {field} field must contain a number greater than or equal to {param}.',
@@ -43,6 +44,7 @@
4344
'matches' => 'The {field} field does not match the {param} field.',
4445
'max_length' => 'The {field} field cannot exceed {param} characters in length.',
4546
'min_length' => 'The {field} field must be at least {param} characters in length.',
47+
'not_equals' => 'The {field} field cannot be: {param}.',
4648
'numeric' => 'The {field} field must contain only numbers.',
4749
'regex_match' => 'The {field} field is not in the correct format.',
4850
'required' => 'The {field} field is required.',

system/Validation/Rules.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ public function differs(string $str = null, string $field, array $data): bool
6565

6666
//--------------------------------------------------------------------
6767

68+
/**
69+
* Equals the static value provided.
70+
*
71+
* @param string $str
72+
* @param string $val
73+
*
74+
* @return boolean
75+
*/
76+
public function equals(string $str = null, string $val): bool
77+
{
78+
return $str === $val;
79+
}
80+
81+
//--------------------------------------------------------------------
82+
6883
/**
6984
* Returns true if $str is $val characters long.
7085
* $val = "5" (one) | "5,8,12" (multiple values)
@@ -261,6 +276,21 @@ public function min_length(string $str = null, string $val, array $data): bool
261276

262277
//--------------------------------------------------------------------
263278

279+
/**
280+
* Does not equal the static value provided.
281+
*
282+
* @param string $str
283+
* @param string $val
284+
*
285+
* @return boolean
286+
*/
287+
public function not_equals(string $str = null, string $val): bool
288+
{
289+
return $str !== $val;
290+
}
291+
292+
//--------------------------------------------------------------------
293+
264294
/**
265295
* Required
266296
*

tests/system/Validation/RulesTest.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,157 @@ public function testDiffersFalse()
379379

380380
$this->assertFalse($this->validation->run($data));
381381
}
382+
383+
//--------------------------------------------------------------------
384+
385+
public function testEqualsNull()
386+
{
387+
$data = [
388+
'foo' => null,
389+
];
390+
391+
$this->validation->setRules([
392+
'foo' => 'equals[]',
393+
]);
394+
395+
$this->assertFalse($this->validation->run($data));
396+
}
397+
398+
//--------------------------------------------------------------------
399+
400+
public function testEqualsEmptyIsEmpty()
401+
{
402+
$data = [
403+
'foo' => '',
404+
];
405+
406+
$this->validation->setRules([
407+
'foo' => 'equals[]',
408+
]);
409+
410+
$this->assertTrue($this->validation->run($data));
411+
}
412+
413+
//--------------------------------------------------------------------
414+
415+
public function testEqualsReturnsFalseOnFailure()
416+
{
417+
$data = [
418+
'foo' => 'bar',
419+
];
420+
421+
$this->validation->setRules([
422+
'foo' => 'equals[notbar]',
423+
]);
424+
425+
$this->assertFalse($this->validation->run($data));
426+
}
427+
428+
//--------------------------------------------------------------------
429+
430+
public function testEqualsReturnsTrueOnSuccess()
431+
{
432+
$data = [
433+
'foo' => 'bar',
434+
];
435+
436+
$this->validation->setRules([
437+
'foo' => 'equals[bar]',
438+
]);
439+
440+
$this->assertTrue($this->validation->run($data));
441+
}
442+
443+
//--------------------------------------------------------------------
444+
445+
public function testEqualsReturnsFalseOnCaseMismatch()
446+
{
447+
$data = [
448+
'foo' => 'bar',
449+
];
450+
451+
$this->validation->setRules([
452+
'foo' => 'equals[Bar]',
453+
]);
454+
455+
$this->assertFalse($this->validation->run($data));
456+
}
457+
458+
//--------------------------------------------------------------------
459+
460+
public function testNotEqualsNull()
461+
{
462+
$data = [
463+
'foo' => null,
464+
];
465+
466+
$this->validation->setRules([
467+
'foo' => 'not_equals[]',
468+
]);
469+
470+
$this->assertTrue($this->validation->run($data));
471+
}
472+
473+
//--------------------------------------------------------------------
474+
475+
public function testNotEqualsEmptyIsEmpty()
476+
{
477+
$data = [
478+
'foo' => '',
479+
];
480+
481+
$this->validation->setRules([
482+
'foo' => 'not_equals[]',
483+
]);
484+
485+
$this->assertFalse($this->validation->run($data));
486+
}
487+
488+
//--------------------------------------------------------------------
489+
490+
public function testNotEqualsReturnsFalseOnFailure()
491+
{
492+
$data = [
493+
'foo' => 'bar',
494+
];
495+
496+
$this->validation->setRules([
497+
'foo' => 'not_equals[bar]',
498+
]);
499+
500+
$this->assertFalse($this->validation->run($data));
501+
}
502+
503+
//--------------------------------------------------------------------
504+
505+
public function testNotEqualsReturnsTrueOnSuccess()
506+
{
507+
$data = [
508+
'foo' => 'bar',
509+
];
510+
511+
$this->validation->setRules([
512+
'foo' => 'not_equals[notbar]',
513+
]);
514+
515+
$this->assertTrue($this->validation->run($data));
516+
}
517+
518+
//--------------------------------------------------------------------
519+
520+
public function testNotEqualsReturnsTrueOnCaseMismatch()
521+
{
522+
$data = [
523+
'foo' => 'bar',
524+
];
525+
526+
$this->validation->setRules([
527+
'foo' => 'not_equals[Bar]',
528+
]);
529+
530+
$this->assertTrue($this->validation->run($data));
531+
}
532+
382533

383534
//--------------------------------------------------------------------
384535

user_guide_src/source/libraries/validation.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,8 @@ alpha_numeric No Fails if field contains anything other than
661661
alpha_numeric_space No Fails if field contains anything other than alpha-numeric characters, numbers or space.
662662
decimal No Fails if field contains anything other than a decimal number.
663663
differs Yes Fails if field does not differ from the one in the parameter. differs[field_name]
664-
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]
664+
equals Yes Fails if field is not exactly the parameter value.
665+
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]
665666
greater_than Yes Fails if field is less than or equal to the parameter value or not numeric. greater_than[8]
666667
greater_than_equal_to Yes Fails if field is less than the parameter value, or not numeric. greater_than_equal_to[5]
667668
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
673674
matches Yes The value must match the value of the field in the parameter. matches[field]
674675
max_length Yes Fails if field is longer than the parameter value. max_length[8]
675676
min_length Yes Fails if field is shorter than the parameter value. min_length[3]
677+
not_equals Yes Fails if field is exactly the parameter value.
676678
numeric No Fails if field contains anything other than numeric characters.
677679
regex_match Yes Fails if field does not match the regular expression. regex_match[/regex/]
678680
if_exist No If this rule is present, validation will only return possible errors if the field key exists,

0 commit comments

Comments
 (0)