|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace mindplay\kissform\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use mindplay\kissform\Field; |
|
6
|
|
|
use mindplay\kissform\InputModel; |
|
7
|
|
|
use mindplay\kissform\InputRenderer; |
|
8
|
|
|
use mindplay\kissform\Validators\CheckLength; |
|
9
|
|
|
use mindplay\kissform\Validators\CheckMaxLength; |
|
10
|
|
|
use mindplay\kissform\Validators\CheckMinLength; |
|
11
|
|
|
use mindplay\kissform\Validators\CheckPattern; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* This class provides information about a text field, e.g. a plain |
|
15
|
|
|
* input type=text element. |
|
16
|
|
|
*/ |
|
17
|
|
|
class TextField extends Field |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var int|null |
|
21
|
|
|
*/ |
|
22
|
|
|
public $min_length; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var int|null |
|
26
|
|
|
*/ |
|
27
|
|
|
public $max_length; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|null |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $pattern; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var string|null |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $pattern_error; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $pattern regular expression pattern (optional; no delimiters, modifiers or anchors) |
|
41
|
|
|
* @param string $error error message to apply on pattern mismatch |
|
42
|
|
|
*/ |
|
43
|
5 |
|
public function setPattern($pattern, $error) |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->pattern = $pattern; |
|
46
|
5 |
|
$this->pattern_error = $error; |
|
47
|
5 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
|
53
|
|
|
{ |
|
54
|
6 |
|
$defaults = []; |
|
55
|
|
|
|
|
56
|
6 |
|
if ($this->max_length) { |
|
|
|
|
|
|
57
|
1 |
|
$defaults['maxlength'] = $this->max_length; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
6 |
|
if ($this->pattern) { |
|
|
|
|
|
|
61
|
1 |
|
$defaults['pattern'] = $this->pattern; |
|
62
|
1 |
|
$defaults['data-pattern-error'] = $this->pattern_error; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
6 |
|
return $renderer->inputFor($this, 'text', $attr + $defaults); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
8 |
|
public function createValidators() |
|
72
|
|
|
{ |
|
73
|
8 |
|
$validators = parent::createValidators(); |
|
74
|
|
|
|
|
75
|
8 |
|
if ($this->min_length !== null) { |
|
76
|
7 |
|
if ($this->max_length !== null) { |
|
77
|
7 |
|
$validators[] = new CheckLength($this->min_length, $this->max_length); |
|
78
|
|
|
} else { |
|
79
|
7 |
|
$validators[] = new CheckMinLength($this->min_length); |
|
80
|
|
|
} |
|
81
|
8 |
|
} else if ($this->max_length !== null) { |
|
82
|
7 |
|
$validators[] = new CheckMaxLength($this->max_length); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
8 |
|
if ($this->pattern) { |
|
|
|
|
|
|
86
|
4 |
|
$validators[] = new CheckPattern("/^{$this->pattern}$/", $this->pattern_error); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
8 |
|
return $validators; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: