|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\kissform; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\kissform\Facets\FieldInterface; |
|
6
|
|
|
use mindplay\kissform\Facets\ValidatorInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This class represents the validation state of an input model. |
|
10
|
|
|
* |
|
11
|
|
|
* Errors accumulate internally - only the first error encountered (for a given property) |
|
12
|
|
|
* is recorded, since commonly multiple error-messages for the same property are |
|
13
|
|
|
* of no practical help to the end-user. |
|
14
|
|
|
* |
|
15
|
|
|
* By default {@see Field::$label} is used when referring to fields in error messages, |
|
16
|
|
|
* but you can override these names using {@see InputValidation::setTitle()}. |
|
17
|
|
|
*/ |
|
18
|
|
|
class InputValidation |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var InputModel input model |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $model; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string[] map where field name => display name override |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $titles = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The given input/model is assumed to be valid |
|
32
|
|
|
* |
|
33
|
|
|
* @param InputModel|array|null $model the form input to be validated |
|
34
|
|
|
*/ |
|
35
|
18 |
|
public function __construct($model) |
|
36
|
|
|
{ |
|
37
|
18 |
|
$this->model = InputModel::create($model); |
|
38
|
|
|
|
|
39
|
18 |
|
$this->model->clearErrors(true); |
|
40
|
18 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Produce a title for a given Field. |
|
44
|
|
|
* |
|
45
|
|
|
* @param FieldInterface $field |
|
46
|
|
|
* |
|
47
|
|
|
* @return string label |
|
48
|
|
|
* |
|
49
|
|
|
* @see setLabel() |
|
50
|
|
|
* @see Field::getLabel() |
|
51
|
|
|
*/ |
|
52
|
15 |
|
public function getLabel(FieldInterface $field) |
|
53
|
|
|
{ |
|
54
|
15 |
|
return array_key_exists($field->getName(), $this->titles) |
|
55
|
1 |
|
? $this->titles[$field->getName()] |
|
56
|
15 |
|
: $field->getLabel(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Override the display name used when referring to a given Field in error messages |
|
61
|
|
|
* |
|
62
|
|
|
* @param FieldInterface $field |
|
63
|
|
|
* @param string $title display name override |
|
64
|
|
|
* |
|
65
|
|
|
* @return $this |
|
66
|
|
|
* |
|
67
|
|
|
* @see getLabel() |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function setLabel(FieldInterface $field, $title) |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->titles[$field->getName()] = $title; |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Check the basic constraints defined by the Field itself, validating for e.g. required input, |
|
78
|
|
|
* data-types and value ranges. |
|
79
|
|
|
* |
|
80
|
|
|
* @param FieldInterface|FieldInterface[] $field Field(s) to check |
|
81
|
|
|
* |
|
82
|
|
|
* @return $this |
|
83
|
|
|
* |
|
84
|
|
|
* @see Field::createValidators() |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function check($field) |
|
87
|
|
|
{ |
|
88
|
|
|
/** @var FieldInterface[] $fields */ |
|
89
|
2 |
|
$fields = is_array($field) ? $field : [$field]; |
|
90
|
|
|
|
|
91
|
2 |
|
foreach ($fields as $field) { |
|
92
|
2 |
|
$this->validate($field, $field->createValidators()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Validates one or more Fields using one or more given Validators. |
|
100
|
|
|
* |
|
101
|
|
|
* Consider calling {@see check()} first to validate the Field's basic constraints. |
|
102
|
|
|
* |
|
103
|
|
|
* @param FieldInterface|FieldInterface[] $field Field(s) to validate |
|
104
|
|
|
* @param ValidatorInterface|ValidatorInterface[] $validator one or more Validators to apply |
|
105
|
|
|
* |
|
106
|
|
|
* @return $this |
|
107
|
|
|
* |
|
108
|
|
|
* @see check() |
|
109
|
|
|
*/ |
|
110
|
18 |
|
public function validate($field, $validator) |
|
111
|
|
|
{ |
|
112
|
|
|
/** @var FieldInterface[] $fields */ |
|
113
|
18 |
|
$fields = is_array($field) ? $field : [$field]; |
|
114
|
|
|
|
|
115
|
18 |
|
foreach ($fields as $field) { |
|
116
|
|
|
/** @var ValidatorInterface[] $validators */ |
|
117
|
18 |
|
$validators = is_array($validator) ? $validator : [$validator]; |
|
118
|
|
|
|
|
119
|
18 |
|
foreach ($validators as $validator) { |
|
120
|
18 |
|
$validator->validate($field, $this->model, $this); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
18 |
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|