1
|
|
|
<?php |
2
|
|
|
namespace Ajax\semantic\html\collections\form; |
3
|
|
|
|
4
|
|
|
use Ajax\JsUtils; |
5
|
|
|
use Ajax\semantic\components\validation\Rule; |
6
|
|
|
use Ajax\semantic\html\base\HtmlSemDoubleElement; |
7
|
|
|
use Ajax\semantic\html\base\constants\Wide; |
8
|
|
|
use Ajax\semantic\html\base\constants\State; |
9
|
|
|
use Ajax\semantic\html\base\constants\Direction; |
10
|
|
|
use Ajax\semantic\html\elements\HtmlLabel; |
11
|
|
|
use Ajax\semantic\components\validation\FieldValidation; |
12
|
|
|
use Ajax\semantic\html\collections\form\traits\FieldTrait; |
13
|
|
|
use Ajax\semantic\html\base\constants\Size; |
14
|
|
|
|
15
|
|
|
class HtmlFormField extends HtmlSemDoubleElement { |
16
|
|
|
use FieldTrait; |
17
|
|
|
|
18
|
|
|
protected $_container; |
19
|
|
|
|
20
|
|
|
protected $_validation; |
21
|
|
|
|
22
|
|
|
public function __construct($identifier, $field, $label = NULL) { |
23
|
|
|
parent::__construct($identifier, "div", "field"); |
24
|
|
|
$this->content = array(); |
25
|
|
|
$this->_states = [ |
26
|
|
|
State::ERROR, |
27
|
|
|
State::DISABLED |
28
|
|
|
]; |
29
|
|
|
if (isset($label) && $label !== "") |
30
|
|
|
$this->setLabel($label); |
31
|
|
|
$this->setField($field); |
32
|
|
|
$this->_validation = NULL; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function addPointingLabel($label, $pointing = Direction::NONE) { |
36
|
|
|
$labelO = new HtmlLabel("", $label); |
37
|
|
|
$labelO->setPointing($pointing); |
38
|
|
|
$this->addContent($labelO, $pointing === "below" || $pointing === "right"); |
39
|
|
|
return $labelO; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function setLabel($label) { |
43
|
|
|
$labelO = $label; |
44
|
|
|
if (\is_string($label)) { |
45
|
|
|
$labelO = new HtmlSemDoubleElement("", "label", ""); |
46
|
|
|
$labelO->setContent($label); |
47
|
|
|
$labelO->setProperty("for", \str_replace("field-", "", $this->identifier)); |
48
|
|
|
} |
49
|
|
|
$this->content["label"] = $labelO; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setField($field) { |
53
|
|
|
$this->content["field"] = $field; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the label or null |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function getLabel() { |
62
|
|
|
if (\array_key_exists("label", $this->content)) |
63
|
|
|
return $this->content["label"]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return the field |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function getField() { |
72
|
|
|
return $this->content["field"]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return the field with data |
77
|
|
|
* |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
public function getDataField() { |
81
|
|
|
return $this->content["field"]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* puts the label before or behind |
86
|
|
|
*/ |
87
|
|
|
public function swapLabel() { |
88
|
|
|
$label = $this->getLabel(); |
89
|
|
|
unset($this->content["label"]); |
90
|
|
|
$this->content["label"] = $label; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Defines the field width |
95
|
|
|
* |
96
|
|
|
* @param int $width |
97
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
98
|
|
|
*/ |
99
|
|
|
public function setWidth($width) { |
100
|
|
|
if (\is_int($width)) { |
|
|
|
|
101
|
|
|
$width = Wide::getConstants()["W" . $width]; |
102
|
|
|
} |
103
|
|
|
$this->addToPropertyCtrl("class", $width, Wide::getConstants()); |
104
|
|
|
if (isset($this->_container)) { |
105
|
|
|
$this->_container->setEqualWidth(false); |
106
|
|
|
} |
107
|
|
|
return $this->addToPropertyCtrl("class", "wide", array( |
108
|
|
|
"wide" |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Field displays an error state |
114
|
|
|
* |
115
|
|
|
* @return \Ajax\semantic\html\collections\form\HtmlFormField |
116
|
|
|
*/ |
117
|
|
|
public function setError() { |
118
|
|
|
return $this->addToProperty("class", "error"); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function setInline() { |
122
|
|
|
return $this->addToProperty("class", "inline"); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function jsState($state) { |
126
|
|
|
return $this->jsDoJquery("addClass", $state); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function setContainer($_container) { |
130
|
|
|
$this->_container = $_container; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function setReadonly() { |
135
|
|
|
$this->getDataField()->setProperty("readonly", ""); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function addRule($type, $prompt = NULL, $value = NULL) { |
139
|
|
|
$field = $this->getDataField(); |
140
|
|
|
if (isset($field)) { |
141
|
|
|
if (! isset($this->_validation)) { |
142
|
|
|
$this->_validation = new FieldValidation($field->getIdentifier()); |
143
|
|
|
} |
144
|
|
|
if($type instanceof Rule){ |
145
|
|
|
if($type->getType()=='empty'){ |
146
|
|
|
$this->addToProperty('class', 'required'); |
147
|
|
|
} |
148
|
|
|
}elseif ($type === 'empty' || ($type['type'] ?? '') === 'empty') { |
149
|
|
|
$this->addToProperty('class', 'required'); |
150
|
|
|
} |
151
|
|
|
$this->_validation->addRule($type, $prompt, $value); |
152
|
|
|
} |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function setOptional($optional = true) { |
157
|
|
|
$field = $this->getDataField(); |
158
|
|
|
if (isset($field)) { |
159
|
|
|
if (! isset($this->_validation)) { |
160
|
|
|
$this->_validation = new FieldValidation($field->getIdentifier()); |
161
|
|
|
} |
162
|
|
|
$this->_validation->setOptional($optional); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function addRules(array $rules) { |
167
|
|
|
foreach ($rules as $rule) { |
168
|
|
|
$this->addRule($rule); |
169
|
|
|
} |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function setRules(array $rules) { |
174
|
|
|
$this->_validation = null; |
175
|
|
|
return $this->addRules($rules); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function addIcon($icon, $direction = Direction::LEFT) { |
179
|
|
|
$field = $this->getField(); |
180
|
|
|
return $field->addIcon($icon, $direction); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getValidation() { |
184
|
|
|
return $this->_validation; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function setSize($size) { |
188
|
|
|
return $this->getField()->addToPropertyCtrl("class", $size, Size::getConstants()); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function run(JsUtils $js) { |
192
|
|
|
if (isset($this->_validation)) { |
193
|
|
|
$this->_validation->compile($js); |
194
|
|
|
} |
195
|
|
|
return parent::run($js); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|