Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/view/form/control/AphrontFormControl.php
12262 views
1
<?php
2
3
abstract class AphrontFormControl extends AphrontView {
4
5
private $label;
6
private $caption;
7
private $error;
8
private $name;
9
private $value;
10
private $disabled;
11
private $id;
12
private $controlID;
13
private $controlStyle;
14
private $required;
15
private $hidden;
16
private $classes;
17
18
public function setHidden($hidden) {
19
$this->hidden = $hidden;
20
return $this;
21
}
22
23
public function setID($id) {
24
$this->id = $id;
25
return $this;
26
}
27
28
public function getID() {
29
return $this->id;
30
}
31
32
public function setControlID($control_id) {
33
$this->controlID = $control_id;
34
return $this;
35
}
36
37
public function getControlID() {
38
return $this->controlID;
39
}
40
41
public function setControlStyle($control_style) {
42
$this->controlStyle = $control_style;
43
return $this;
44
}
45
46
public function getControlStyle() {
47
return $this->controlStyle;
48
}
49
50
public function setLabel($label) {
51
$this->label = $label;
52
return $this;
53
}
54
55
public function getLabel() {
56
return $this->label;
57
}
58
59
public function setCaption($caption) {
60
$this->caption = $caption;
61
return $this;
62
}
63
64
public function getCaption() {
65
return $this->caption;
66
}
67
68
public function setError($error) {
69
$this->error = $error;
70
return $this;
71
}
72
73
public function getError() {
74
return $this->error;
75
}
76
77
public function setName($name) {
78
$this->name = $name;
79
return $this;
80
}
81
82
public function getName() {
83
return $this->name;
84
}
85
86
public function setValue($value) {
87
$this->value = $value;
88
return $this;
89
}
90
91
public function getValue() {
92
return $this->value;
93
}
94
95
public function isValid() {
96
if ($this->error && $this->error !== true) {
97
return false;
98
}
99
100
if ($this->isRequired() && $this->isEmpty()) {
101
return false;
102
}
103
104
return true;
105
}
106
107
public function isRequired() {
108
return $this->required;
109
}
110
111
public function isEmpty() {
112
return $this->getValue() === null || !strlen($this->getValue());
113
}
114
115
public function getSerializedValue() {
116
return $this->getValue();
117
}
118
119
public function readSerializedValue($value) {
120
$this->setValue($value);
121
return $this;
122
}
123
124
public function readValueFromRequest(AphrontRequest $request) {
125
$this->setValue($request->getStr($this->getName()));
126
return $this;
127
}
128
129
public function readValueFromDictionary(array $dictionary) {
130
$this->setValue(idx($dictionary, $this->getName()));
131
return $this;
132
}
133
134
public function setDisabled($disabled) {
135
$this->disabled = $disabled;
136
return $this;
137
}
138
139
public function getDisabled() {
140
return $this->disabled;
141
}
142
143
abstract protected function renderInput();
144
abstract protected function getCustomControlClass();
145
146
protected function shouldRender() {
147
return true;
148
}
149
150
public function addClass($class) {
151
$this->classes[] = $class;
152
return $this;
153
}
154
155
final public function render() {
156
if (!$this->shouldRender()) {
157
return null;
158
}
159
160
$custom_class = $this->getCustomControlClass();
161
162
// If we don't have an ID yet, assign an automatic one so we can associate
163
// the label with the control. This allows assistive technologies to read
164
// form labels.
165
if (!$this->getID()) {
166
$this->setID(celerity_generate_unique_node_id());
167
}
168
169
$input = phutil_tag(
170
'div',
171
array('class' => 'aphront-form-input'),
172
$this->renderInput());
173
174
$error = $this->error;
175
if ($error !== null && strlen($error)) {
176
if ($error === true) {
177
$error = phutil_tag(
178
'span',
179
array('class' => 'aphront-form-error aphront-form-required'),
180
pht('Required'));
181
} else {
182
$error = phutil_tag(
183
'span',
184
array('class' => 'aphront-form-error'),
185
$error);
186
}
187
} else {
188
$error = null;
189
}
190
191
$label = $this->label;
192
if ($label !== null && strlen($label)) {
193
$label = phutil_tag(
194
'label',
195
array(
196
'class' => 'aphront-form-label',
197
'for' => $this->getID(),
198
),
199
array(
200
$this->getLabel(),
201
$error,
202
));
203
} else {
204
$label = null;
205
$custom_class .= ' aphront-form-control-nolabel';
206
}
207
208
$caption = $this->caption;
209
if ($caption !== null && strlen($caption)) {
210
$caption = phutil_tag(
211
'div',
212
array('class' => 'aphront-form-caption'),
213
$this->getCaption());
214
} else {
215
$caption = null;
216
}
217
218
$classes = array();
219
$classes[] = 'aphront-form-control';
220
$classes[] = 'grouped';
221
$classes[] = $custom_class;
222
if ($this->classes) {
223
foreach ($this->classes as $class) {
224
$classes[] = $class;
225
}
226
}
227
228
$style = $this->controlStyle;
229
if ($this->hidden) {
230
$style = 'display: none; '.$style;
231
}
232
233
return phutil_tag(
234
'div',
235
array(
236
'class' => implode(' ', $classes),
237
'id' => $this->controlID,
238
'style' => $style,
239
),
240
array(
241
$label,
242
$error,
243
$input,
244
$caption,
245
));
246
}
247
}
248
249