Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathText.php
More file actions
298 lines (253 loc) · 12 KB
/
Copy pathText.php
File metadata and controls
298 lines (253 loc) · 12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
* ValidForm Builder - build valid and secure web forms quickly
*
* Copyright (c) 2009-2025 Neverwoods Internet Technology - http://neverwoods.com
*
* Felix Langfeldt <felix@neverwoods.com>
* Robin van Baalen <robin@stylr.nl>
*
* All rights reserved.
*
* This software is released under the GNU GPL v2 License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
*
* @package ValidForm
* @author Felix Langfeldt <felix@neverwoods.com>
* @author Robin van Baalen <robin@stylr.nl>
* @copyright 2009-2025 Neverwoods Internet Technology - http://neverwoods.com
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU GPL v2
* @link http://validformbuilder.org
*/
namespace ValidFormBuilder;
/**
* Text Class is the most used.
*
* Text objects are used to create input[type='text'] fields.
*
* #### Example; Add a text field with some validation and custom classes to the form.
* ```php
* $objForm->addField(
* "first-name",
* "First name",
* ValidForm::VFORM_STRING,
* array(
* // Make this field required
* "required" => true,
* // It should have a maximum of 10 characters
* "maxLength" => 10,
* // It should have a minimum of 3 characters
* "minLength" => 3
* ),
* array(
* // Error message when required state isn't met
* "required" => "This is a required field",
* // Error message when input length is larger than 10 characters
* "maxLength" => "Maximum of %s characters allowed.",
* // Error message when input length is shorter than 3 characters
* "minLength" => "Minimum of %s characters required."
* ),
* array(
* // Add a custom class to the input element
* // This results in something like
* // <input type='text' class='vf__text custom-class'>
* "fieldclass" => "custom-class",
* // Add a custom class to the field container
* // This results in
* // <div class='vf__required container-class'>
* // <input type='text'>
* // </div>
* "class" => "container-class"
*
* )
* );
* ```
*
* @package ValidForm
* @author Felix Langfeldt <felix@neverwoods.com>
* @author Robin van Baalen <robin@stylr.nl>
* @version 5.3.0
*/
class Text extends Element
{
/**
* @see \ValidFormBuilder\Element::toHtml()
*/
public function toHtml($submitted = false, $blnSimpleLayout = false, $blnLabel = true, $blnDisplayErrors = true)
{
$strOutput = "";
$intDynamicCount = $this->getDynamicCount();
for ($intCount = 0; $intCount <= $intDynamicCount; $intCount ++) {
$strOutput .= $this->__toHtml($submitted, $blnSimpleLayout, $blnLabel, $blnDisplayErrors, $intCount);
}
return $strOutput;
}
/**
* @see \ValidFormBuilder\Element::__toHtml()
*/
public function __toHtml($submitted = false, $blnSimpleLayout = false, $blnLabel = true, $blnDisplayErrors = true, $intCount = 0)
{
$strName = ($intCount == 0) ? $this->__name : $this->__name . "_" . $intCount;
$strId = ($intCount == 0) ? $this->__id : $this->__id . "_" . $intCount;
$varValue = $this->__getValue($submitted, $intCount);
$blnError = ($submitted && ! $this->__validator->validate($intCount) && $blnDisplayErrors) ? true : false;
if (! $blnSimpleLayout) {
// *** We asume that all dynamic fields greater than 0 are never required.
if ($this->__validator->getRequired() && $intCount == 0) {
$this->setMeta("class", "vf__required");
} else {
$this->setMeta("class", "vf__optional");
}
if ($this->isRemovable()) {
$this->setMeta("class", "vf__removable");
}
//*** Add data-dynamic="original" or data-dynamic="clone" attributes to dynamic fields
if ($this->isDynamic()) {
if ($intCount === 0) {
// This is the first, original element. Make sure to define that.
$this->setMeta('data-dynamic', 'original', true);
} else {
$this->setMeta('data-dynamic', 'clone', true);
$this->setMeta("class", "vf__clone");
}
}
// *** Set custom meta.
if ($blnError) {
$this->setMeta("class", "vf__error");
}
if (! $blnLabel) {
$this->setMeta("class", "vf__nolabel");
}
if (! empty($this->__hint) && ($varValue == $this->__hint)) {
$this->setMeta("class", "vf__hint");
}
// Call this right before __getMetaString();
$this->setConditionalMeta();
$strOutput = "<div{$this->__getMetaString()}>\n";
if ($blnError) {
$strOutput .= "<p class=\"vf__error\">{$this->__validator->getError($intCount)}</p>";
}
if ($blnLabel) {
$strLabel = (! empty($this->__requiredstyle) && $this->__validator->getRequired()) ? sprintf($this->__requiredstyle, $this->__label) : $this->__label;
if (! empty($this->__label)) {
$strOutput .= "<label for=\"{$strId}\"{$this->__getLabelMetaString()}>{$strLabel}</label>\n";
}
}
} else {
if (! empty($this->__hint) && ($varValue == $this->__hint)) {
$this->setMeta("class", "vf__hint");
}
if ($blnError) {
$this->setMeta("class", "vf__error");
}
$this->setMeta("class", "vf__multifielditem");
if ($this->isRemovable()) {
$this->setMeta("class", "vf__removable");
}
//*** Add data-dynamic="original" or data-dynamic="clone" attributes to dynamic fields
if ($this->isDynamic()) {
if ($intCount === 0) {
// This is the first, original element. Make sure to define that.
$this->setMeta('data-dynamic', 'original', true);
} else {
$this->setMeta('data-dynamic', 'clone', true);
$this->setMeta("class", "vf__clone");
}
}
// Call this right before __getMetaString();
$this->setConditionalMeta();
$strOutput = "<div{$this->__getMetaString()}>\n";
}
// *** Add max-length attribute to the meta array. This is being read by the getMetaString method.
if ($this->__validator->getMaxLength() > 0) {
$this->setFieldMeta("maxlength", $this->__validator->getMaxLength());
}
$varValue = htmlspecialchars((string)$varValue, ENT_QUOTES);
$strOutput .= "<input type=\"text\" value=\"{$varValue}\" name=\"{$strName}\" id=\"{$strId}\"{$this->__getFieldMetaString()} />\n";
if (! empty($this->__tip)) {
$this->setTipMeta("class", "vf__tip");
$strOutput .= "<small{$this->__getTipMetaString()}>{$this->__tip}</small>\n";
}
if ($this->isRemovable()) {
$this->setMeta("dynamicRemoveLabelClass", "vf__removeLabel");
$strOutput .= $this->getRemoveLabelHtml();
}
$strOutput .= "</div>\n";
if (!$blnSimpleLayout && $intCount == $this->getDynamicCount()) {
$strOutput .= $this->getDynamicHtml();
}
return $strOutput;
}
/**
* @see \ValidFormBuilder\Element::toJS()
*/
public function toJS($intDynamicPosition = false)
{
$strOutput = "";
$strCheck = $this->__sanitizeCheckForJs($this->__validator->getCheck());
$strRequired = ($this->__validator->getRequired()) ? "true" : "false";
$intMaxLength = ($this->__validator->getMaxLength() > 0) ? $this->__validator->getMaxLength() : "null";
$intMinLength = ($this->__validator->getMinLength() > 0) ? $this->__validator->getMinLength() : "null";
$strSanitize = "null";
if (is_array($this->__validator->getSanitisers())) {
$strSanitize = json_encode($this->__validator->getSanitisers());
}
$strExternalValidation = "null";
if (is_array($this->__validator->getExternalValidation())) {
$arrExternalValidation = $this->__validator->getExternalValidation();
if (isset($arrExternalValidation['javascript'])) {
$strExternalValidation = json_encode($arrExternalValidation['javascript']);
}
}
$fltMinValue = (is_null($this->__validator->getMinValue())) ? "null" : $this->__validator->getMinValue();
$fltMaxValue = (is_null($this->__validator->getMaxValue())) ? "null" : $this->__validator->getMaxValue();
if ($this->__dynamic || $intDynamicPosition) {
$intDynamicCount = $this->getDynamicCount($intDynamicPosition);
for ($intCount = 0; $intCount <= $intDynamicCount; $intCount ++) {
$strId = ($intCount == 0) ? $this->__id : $this->__id . "_" . $intCount;
$strName = ($intCount == 0) ? $this->__name : $this->__name . "_" . $intCount;
// *** We asume that all dynamic fields greater than 0 are never required.
if ($intDynamicCount > 0) {
$strRequired = "false";
}
$strOutput .= "objForm.addElement('{$strId}', '{$strName}', {$strCheck}, {$strRequired}, {$intMaxLength}, {$intMinLength}, '"
. addslashes((string)$this->__validator->getFieldHint()) . "', '"
. addslashes((string)$this->__validator->getTypeError()) . "', '"
. addslashes((string)$this->__validator->getRequiredError()) . "', '"
. addslashes((string)$this->__validator->getHintError()) . "', '"
. addslashes((string)$this->__validator->getMinLengthError()) . "', '"
. addslashes((string)$this->__validator->getMaxLengthError()) . "', "
. $strSanitize . ", "
. $strExternalValidation . ", '"
. addslashes((string)$this->__validator->getExternalValidationError()) . "', "
. $fltMinValue . ", '"
. addslashes((string)$this->__validator->getMinValueError()) . "', "
. $fltMaxValue . ", '"
. addslashes((string)$this->__validator->getMaxValueError()) . "');\n";
// *** MatchWith logic per dynamic field.
$strOutput .= $this->matchWithToJs($intCount);
// *** Render the condition logic per dynamic field.
$strOutput .= $this->conditionsToJs($intCount);
}
} else {
$strOutput = "objForm.addElement('{$this->__id}', '{$this->__name}', {$strCheck}, {$strRequired}, {$intMaxLength}, {$intMinLength}, '"
. addslashes((string)$this->__validator->getFieldHint()) . "', '"
. addslashes((string)$this->__validator->getTypeError()) . "', '"
. addslashes((string)$this->__validator->getRequiredError()) . "', '"
. addslashes((string)$this->__validator->getHintError()) . "', '"
. addslashes((string)$this->__validator->getMinLengthError()) . "', '"
. addslashes((string)$this->__validator->getMaxLengthError()) . "', "
. $strSanitize . ", "
. $strExternalValidation . ", '"
. addslashes((string)$this->__validator->getExternalValidationError()) . "', "
. $fltMinValue . ", '"
. addslashes((string)$this->__validator->getMinValueError()) . "', "
. $fltMaxValue . ", '"
. addslashes((string)$this->__validator->getMaxValueError()) . "');\n";
// *** MatchWith logic.
$strOutput .= $this->matchWithToJs();
// *** Condition logic.
$strOutput .= $this->conditionsToJs();
}
return $strOutput;
}
}
You can’t perform that action at this time.
