Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
form.php
1<?php
3
9
14class Form implements IErrorable
15{
16 private $id;
17 private $name;
18 private $action;
19 private $method;
20 private $class = array();
21 private $prefix = '';
22 private $postfix = '';
23 private $dataAttributes = array();
24
25
26 private $fields = array();
27
28 private $errors = array();
29
30
37 public function add($field)
38 {
39 if (!($field instanceof Base))
40 {
41 $field = $this->convertToField($field);
42 }
43
44 if ($field->getKey())
45 {
46 $this->fields[$field->getKey()] = $field;
47 }
48 else
49 {
50 $this->fields[] = $field;
51 }
52 $field->setForm($this);
53 return $this;
54 }
55
63 public function addFieldBefore($newField, $targetField)
64 {
65 if (!($newField instanceof Base))
66 {
67 $newField = $this->convertToField($newField);
68 }
69
70 $indexToInsert = null;
71 $newFieldsList = array();
72 foreach ($this->fields as $key => $field)
73 {
74 if ($field === $targetField)
75 {
76 $newField->setForm($this);
77 if ($newField->getKey())
78 {
79 $newFieldsList[$newField->getKey()] = $newField;
80 }
81 else
82 {
83 $newFieldsList[] = $newField;
84 }
85 }
86 $newFieldsList[$key] = $field;
87 }
88
89 $this->fields = $newFieldsList;
90 }
91
99 public function addFieldAfter($newField, $targetField)
100 {
101 if (!($newField instanceof Base))
102 {
103 $newField = $this->convertToField($newField);
104 }
105
106 $indexToInsert = null;
107 $newFieldsList = array();
108 foreach ($this->fields as $key => $field)
109 {
110 $newFieldsList[$key] = $field;
111 if ($field === $targetField)
112 {
113 $newField->setForm($this);
114 if ($newField->getKey())
115 {
116 $newFieldsList[$newField->getKey()] = $newField;
117 }
118 else
119 {
120 $newFieldsList[] = $newField;
121 }
122 }
123 }
124
125 $this->fields = $newFieldsList;
126
127 }
128
135 private function convertToField($options)
136 {
137 if (is_string($options) || is_int($options))
138 {
139 return new Html($options);
140 }
141 else
142 {
143 throw new ArgumentException('Can\'t convert to form element');
144 }
145 }
149 public function getFields()
150 {
151 return $this->fields;
152 }
153
158 public function getField($key)
159 {
160 static $fields;
161 if (!$fields)
162 {
163 $fields = $this->getFields();
164 }
165 if (!isset($fields[$key]))
166 {
167 $this->errors[] = new Error('No field with key:' . $key);
168 $result = null;
169 }
170 else
171 {
172 $result = $fields[$key];
173 }
174 return $result;
175 }
176
177
184 public function render()
185 {
186 echo htmlspecialcharsbx($this->getPrefix());
187 $action = $this->getAction();
188 $id = $this->getId();
189 $class = $this->getClass();
190 $name = $this->getName();
191 $dataAttributes = $this->getDataAttributes();
192 $dataAttributesString = '';
193 foreach ($dataAttributes as $key => $value)
194 {
195 $dataAttributesString .= ' data-' . $key . '="' . $value . '" ';
196 }
197
198 $formArguments = 'action="' . (($action !== null) ? $action : '#') . '" ';
199 $formArguments .= ($id !== null) ? 'id="' . $id . '" ' : '';
200 $formArguments .= !empty($class) ? 'class="' . implode(' ', $class) . '" ' : '';
201 $formArguments .= ($name !== null) ? 'name="' . $name . '" ' : '';
202 $formArguments .= $dataAttributesString;
203 echo '<form ' . $formArguments . '>';
204
205 $fields = $this->getFields();
206 foreach ($fields as $key => $field)
207 {
208 $field->render();
209 }
210
211 echo '</form>';
212
213 echo htmlspecialcharsbx($this->getPostfix());
214 }
215
219 public function getErrors()
220 {
221 return $this->errors;
222 }
223
227 public function getPrefix()
228 {
229 return $this->prefix;
230 }
231
236 public function setPrefix($prefix)
237 {
238 $this->prefix = $prefix;
239 return $this;
240 }
241
242
246 public function getPostfix()
247 {
248 return $this->postfix;
249 }
250
255 public function setPostfix($postfix)
256 {
257 $this->postfix = $postfix;
258 return $this;
259 }
260
265 public function addClass($className)
266 {
267 $this->class[] = $className;
268 }
269
273 public function getClass()
274 {
275 return $this->class;
276 }
277
282 public function setClass($class)
283 {
284 $this->class = $class;
285 }
286
290 public function getName()
291 {
292 return $this->name;
293 }
294
299 public function setName($name)
300 {
301 $this->name = $name;
302 }
303
307 public function getId()
308 {
309 return $this->id;
310 }
311
316 public function setId($id)
317 {
318 $this->id = $id;
319 }
320
326 public function getAction()
327 {
328 return $this->action;
329 }
330
335 public function setAction($action)
336 {
337 $this->action = $action;
338 return $this;
339 }
340
344 public function getMethod()
345 {
346 return $this->method;
347 }
348
353 public function setMethod($method)
354 {
355 $this->method = $method;
356 return $this;
357 }
358
362 public function getDataAttributes()
363 {
364 return $this->dataAttributes;
365 }
366
371 public function setDataAttributes($dataAttributes)
372 {
373 $this->dataAttributes = $dataAttributes;
374 return $this;
375 }
376
382 public function addDataAttribute($key, $value = '')
383 {
384 $this->dataAttributes[$key] = $value;
385 }
386}
setDataAttributes($dataAttributes)
Definition form.php:371
addFieldAfter($newField, $targetField)
Definition form.php:99
addDataAttribute($key, $value='')
Definition form.php:382
addFieldBefore($newField, $targetField)
Definition form.php:63