1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ntentan\honam\engines\php\helpers\form; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* The form field class. |
8
|
|
|
* This class represents a form field element. Subclasses of this class are to be used to capture information from the |
9
|
|
|
* user of the application. |
10
|
|
|
*/ |
11
|
|
|
class Field extends Element |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* A flag for setting the required state of the form. If this value |
15
|
|
|
* is set as true then the form would not be validated if there is |
16
|
|
|
* no value entered into this field. |
17
|
|
|
*/ |
18
|
|
|
public $required = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The value of the form field. |
22
|
|
|
*/ |
23
|
|
|
protected $value; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The constructor for the field element. |
27
|
|
|
*/ |
28
|
|
|
public function __construct(string $name = "", string $value = "") |
29
|
|
|
{ |
30
|
|
|
$this->name = $name; |
|
|
|
|
31
|
|
|
$this->value = $value; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets the value of the field. |
36
|
|
|
* |
37
|
|
|
* @param string $value The value of the field. |
38
|
|
|
* @return Field |
39
|
|
|
*/ |
40
|
|
|
public function setValue(string $value): Field |
41
|
|
|
{ |
42
|
|
|
$this->value = $value; |
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the value of the field. |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function getValue(): ?string |
52
|
|
|
{ |
53
|
|
|
return $this->value; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Sets the required status of the field. |
58
|
|
|
* |
59
|
|
|
* @param boolean $required |
60
|
|
|
* @return Field |
61
|
|
|
*/ |
62
|
|
|
public function setRequired(bool $required): Field |
63
|
|
|
{ |
64
|
|
|
$this->required = $required; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the required status of the field. |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function getRequired(): bool |
74
|
|
|
{ |
75
|
|
|
return $this->required; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $data |
80
|
|
|
*/ |
81
|
|
|
public function setData(array $data): Field |
82
|
|
|
{ |
83
|
|
|
if (array_search($this->getName(), array_keys($data)) !== false) { |
84
|
|
|
$this->setValue($data[$this->getName()]); |
85
|
|
|
} |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function render() |
93
|
|
|
{ |
94
|
|
|
$this->setAttribute("name", $this->getName()); |
95
|
|
|
return $this->templateRenderer->render("input_element.tpl.php", array('element' => $this)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function isContainer() |
99
|
|
|
{ |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..