1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace mindplay\kissform\Fields\Base; |
4
|
|
|
|
5
|
|
|
use mindplay\kissform\Facets\ValidatorInterface; |
6
|
|
|
use mindplay\kissform\Fields\TextField; |
7
|
|
|
use mindplay\kissform\InputModel; |
8
|
|
|
use mindplay\kissform\InputRenderer; |
9
|
|
|
use mindplay\kissform\Validators\CheckMaxValue; |
10
|
|
|
use mindplay\kissform\Validators\CheckMinValue; |
11
|
|
|
use mindplay\kissform\Validators\CheckRange; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract base class for integer, floating-point and fixed-precision numeric Field types. |
15
|
|
|
*/ |
16
|
|
|
abstract class NumericField extends TextField |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int|float|null minimum value |
20
|
|
|
*/ |
21
|
|
|
public $min_value; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* var int|float|null maximum value |
25
|
|
|
*/ |
26
|
|
|
public $max_value; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
2 |
|
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
32
|
|
|
{ |
33
|
2 |
|
$defaults = []; |
34
|
|
|
|
35
|
2 |
|
if ($this->max_length !== null) { |
36
|
2 |
|
$defaults['maxlength'] = $this->max_length; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
if ($this->min_value !== null) { |
40
|
2 |
|
$defaults['min'] = $this->min_value; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
if ($this->max_value !== null) { |
44
|
2 |
|
$defaults['max'] = $this->max_value; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
return $renderer->inputFor($this, 'number', $attr + $defaults); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return ValidatorInterface |
52
|
|
|
*/ |
53
|
|
|
abstract protected function createTypeValidator(); |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
3 |
|
public function createValidators() |
59
|
|
|
{ |
60
|
3 |
|
$validators = parent::createValidators(); |
61
|
|
|
|
62
|
3 |
|
$validators[] = $this->createTypeValidator(); |
63
|
|
|
|
64
|
3 |
|
if ($this->min_value !== null) { |
65
|
2 |
|
if ($this->max_value !== null) { |
66
|
2 |
|
$validators[] = new CheckRange($this->min_value, $this->max_value); |
67
|
|
|
} else { |
68
|
2 |
|
$validators[] = new CheckMinValue($this->min_value); |
69
|
|
|
} |
70
|
3 |
|
} else if ($this->max_value !== null) { |
71
|
2 |
|
$validators[] = new CheckMaxValue($this->max_value); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $validators; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|