1# KeyValue
2
3- `keyValue(string $comparedKey, string $ruleName, string $baseKey)`
4
5Performs validation of `$comparedKey` using the rule named on `$ruleName` with
6`$baseKey` as base.
7
8Sometimes, when validating arrays, the validation of a key value depends on
9another key value and that may cause some ugly code since you need the input
10before the validation, making some checking manually:
11
12```php
13v::key('password')->check($_POST);
14v::key('password_confirmation', v::equals($_POST['password']))->check($_POST);
15```
16
17The problem with the above code is because you do not know if `password` is a
18valid key, so you must check it manually before performing the validation on
19`password_confirmation`.
20
21The `keyValue()` rule makes this job easier by creating a rule named on
22`$ruleName` passing `$baseKey` as the first argument of this rule, see an example:
23
24```php
25v::keyValue('password_confirmation', 'equals', 'password')->validate($_POST);
26```
27
28The above code will result on `true` if _`$_POST['password_confirmation']` is
29[equals](Equals.md) to `$_POST['password']`_, it's the same of:
30
31See another example:
32
33```php
34v::keyValue('state', 'subdivisionCode', 'country')->validate($_POST);
35```
36
37The above code will result on `true` if _`$_POST['state']` is a
38[subdivision code](SubdivisionCode.md) of `$_POST['country']`_:
39
40This rule will invalidate the input if `$comparedKey` or `$baseKey` don't exist,
41or if the rule named on `$ruleName` could not be created (or don't exist).
42
43When using `assert()` or `check()` methods and the rule do not pass, it overwrites
44all values in the validation exceptions with `$baseKey` and `$comparedKey`.
45
46```php
47v::keyValue('password_confirmation', 'equals', 'password')->check($input);
48```
49
50The above code may generate the message:
51
52```
53password_confirmation must be equals "password"
54```
55
56***
57See also:
58
59  * [Key](Key.md)
60  * [KeyNested](KeyNested.md)
61  * [KeySet](KeySet.md)
62
63