Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / view / form / control / AphrontFormControl.php
bloba21cf8164c8e0226ffdb4a694bd563533980a8a1
1 <?php
3 abstract class AphrontFormControl extends AphrontView {
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;
18 public function setHidden($hidden) {
19 $this->hidden = $hidden;
20 return $this;
23 public function setID($id) {
24 $this->id = $id;
25 return $this;
28 public function getID() {
29 return $this->id;
32 public function setControlID($control_id) {
33 $this->controlID = $control_id;
34 return $this;
37 public function getControlID() {
38 return $this->controlID;
41 public function setControlStyle($control_style) {
42 $this->controlStyle = $control_style;
43 return $this;
46 public function getControlStyle() {
47 return $this->controlStyle;
50 public function setLabel($label) {
51 $this->label = $label;
52 return $this;
55 public function getLabel() {
56 return $this->label;
59 public function setCaption($caption) {
60 $this->caption = $caption;
61 return $this;
64 public function getCaption() {
65 return $this->caption;
68 public function setError($error) {
69 $this->error = $error;
70 return $this;
73 public function getError() {
74 return $this->error;
77 public function setName($name) {
78 $this->name = $name;
79 return $this;
82 public function getName() {
83 return $this->name;
86 public function setValue($value) {
87 $this->value = $value;
88 return $this;
91 public function getValue() {
92 return $this->value;
95 public function isValid() {
96 if ($this->error && $this->error !== true) {
97 return false;
100 if ($this->isRequired() && $this->isEmpty()) {
101 return false;
104 return true;
107 public function isRequired() {
108 return $this->required;
111 public function isEmpty() {
112 return $this->getValue() === null || !strlen($this->getValue());
115 public function getSerializedValue() {
116 return $this->getValue();
119 public function readSerializedValue($value) {
120 $this->setValue($value);
121 return $this;
124 public function readValueFromRequest(AphrontRequest $request) {
125 $this->setValue($request->getStr($this->getName()));
126 return $this;
129 public function readValueFromDictionary(array $dictionary) {
130 $this->setValue(idx($dictionary, $this->getName()));
131 return $this;
134 public function setDisabled($disabled) {
135 $this->disabled = $disabled;
136 return $this;
139 public function getDisabled() {
140 return $this->disabled;
143 abstract protected function renderInput();
144 abstract protected function getCustomControlClass();
146 protected function shouldRender() {
147 return true;
150 public function addClass($class) {
151 $this->classes[] = $class;
152 return $this;
155 final public function render() {
156 if (!$this->shouldRender()) {
157 return null;
160 $custom_class = $this->getCustomControlClass();
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());
169 $input = phutil_tag(
170 'div',
171 array('class' => 'aphront-form-input'),
172 $this->renderInput());
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);
187 } else {
188 $error = null;
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(),
199 array(
200 $this->getLabel(),
201 $error,
203 } else {
204 $label = null;
205 $custom_class .= ' aphront-form-control-nolabel';
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;
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;
228 $style = $this->controlStyle;
229 if ($this->hidden) {
230 $style = 'display: none; '.$style;
233 return phutil_tag(
234 'div',
235 array(
236 'class' => implode(' ', $classes),
237 'id' => $this->controlID,
238 'style' => $style,
240 array(
241 $label,
242 $error,
243 $input,
244 $caption,