1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Validation |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2013 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fuel\Validation\Rule; |
12
|
|
|
|
13
|
|
|
use Fuel\Validation\AbstractRule; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Validates that the value of the given field matches the value of another field in the data set. |
17
|
|
|
* |
18
|
|
|
* The parameter of this rule should be the name of the field to match against. |
19
|
|
|
* |
20
|
|
|
* @package Fuel\Validation\Rule |
21
|
|
|
* @author Fuel Development Team |
22
|
|
|
* |
23
|
|
|
* @since 2.0 |
24
|
|
|
*/ |
25
|
|
|
class MatchField extends AbstractRule |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Contains the rule failure message |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $message = 'The field does not match the other given field.'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns true if $value matches the value of the field specified with setParameter() |
37
|
|
|
* |
38
|
|
|
* @param mixed $value Value to validate |
39
|
|
|
* @param string $field Name of the field that is being validated |
40
|
|
|
* @param array $allFields Values of all the other fields being validated |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
* |
44
|
|
|
* @since 2.0 |
45
|
|
|
*/ |
46
|
5 |
|
public function validate($value, $field = null, $allFields = null) |
47
|
|
|
{ |
48
|
5 |
|
$matchAgainst = $this->getParameter(); |
49
|
|
|
|
50
|
|
|
// If any of the needed settings are missing, return false and |
51
|
|
|
// check if the array key exists, if not nothing to validate against |
52
|
5 |
|
if ($allFields === null or $matchAgainst === null or ! isset($allFields[$matchAgainst])) |
53
|
|
|
{ |
54
|
3 |
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
return $allFields[$matchAgainst] == $value; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the field name to match against |
62
|
|
|
* |
63
|
|
|
* @param string $params If an array the first value will be used |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
* |
67
|
|
|
* @since 2.0 |
68
|
|
|
*/ |
69
|
9 |
|
public function setParameter($params) |
70
|
|
|
{ |
71
|
|
|
// Ensure we have only a single thing to match against |
72
|
9 |
|
if (is_array($params)) |
73
|
|
|
{ |
74
|
1 |
|
$params = array_shift($params); |
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
return parent::setParameter($params); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns |
82
|
|
|
* |
83
|
|
|
* array( |
84
|
|
|
* 'field' => <field that will be matched against> |
85
|
|
|
* ); |
86
|
|
|
* |
87
|
|
|
* @return string[] |
88
|
|
|
*/ |
89
|
1 |
|
public function getMessageParameters() |
90
|
|
|
{ |
91
|
|
|
return array( |
92
|
1 |
|
'field' => $this->getParameter(), |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|