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 |
View Code Duplication |
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
|
7 |
|
public function createValidators() |
72
|
|
|
{ |
73
|
7 |
|
$validators = parent::createValidators(); |
74
|
|
|
|
75
|
7 |
|
if ($this->min_length !== null) { |
76
|
6 |
|
if ($this->max_length !== null) { |
77
|
6 |
|
$validators[] = new CheckLength($this->min_length, $this->max_length); |
78
|
|
|
} else { |
79
|
6 |
|
$validators[] = new CheckMinLength($this->min_length); |
80
|
|
|
} |
81
|
7 |
|
} else if ($this->max_length !== null) { |
82
|
6 |
|
$validators[] = new CheckMaxLength($this->max_length); |
83
|
|
|
} |
84
|
|
|
|
85
|
7 |
|
if ($this->pattern) { |
|
|
|
|
86
|
4 |
|
$validators[] = new CheckPattern("/^{$this->pattern}$/", $this->pattern_error); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
return $validators; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.