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