depage-forms
v2.0.0
html forms made easy
Toggle main menu visibility
Loading...
Searching...
No Matches
Text.php
Go to the documentation of this file.
1
<?php
2
10
11
namespace
Depage\HtmlForm\Elements;
12
13
use Depage\HtmlForm\Abstracts;
14
39
class
Text
extends
Abstracts\Input
40
{
44
protected
$placeholder
;
45
49
protected
$normalize
;
50
54
protected
$maxlength
=
false
;
55
59
protected
$list
=
false
;
60
69
public
function
__construct
(
$name
, $parameters, $form)
70
{
71
parent::__construct(
$name
, $parameters, $form);
72
73
$this->list = (isset($parameters[
'list'
]) && is_array($parameters[
'list'
])) ? $parameters[
'list'
] :
false
;
74
}
75
85
protected
function
setDefaults
(): void
86
{
87
parent::setDefaults();
88
89
// textClass elements have values of type string
90
$this->defaults[
'defaultValue'
] =
''
;
91
$this->defaults[
'placeholder'
] =
false
;
92
$this->defaults[
'maxlength'
] =
false
;
93
$this->defaults[
'normalize'
] =
true
;
94
}
95
101
public
function
__toString
(): string
102
{
103
$value
= $this->
htmlValue
();
104
$type
= strtolower($this->type);
105
$inputAttributes = $this->
htmlInputAttributes
();
106
$marker
= $this->
htmlMarker
();
107
$label
= $this->htmlLabel();
108
$list
= $this->
htmlList
();
109
$wrapperAttributes = $this->
htmlWrapperAttributes
();
110
$errorMessage
= $this->
htmlErrorMessage
();
111
$helpMessage
= $this->
htmlHelpMessage
();
112
113
return
"<p {$wrapperAttributes}>"
114
.
"<label>"
115
.
"<span class=\"depage-label\">{$label}{$marker}</span>"
116
.
"<input name=\"{$this->name}\" type=\"{$type}\"{$inputAttributes} value=\"{$value}\">"
117
.
$list
118
.
"</label>"
119
.
$errorMessage
120
.
$helpMessage
121
.
"</p>\n"
;
122
}
123
130
protected
function
htmlList
(?array $options =
null
, array|
string
|
null
$value
=
null
): string
131
{
132
if
($this->list && is_array($this->list)) {
133
$formName
= $this->htmlFormName();
134
$options = $this->
htmlEscape
($this->list);
135
136
$htmlList =
"<datalist id=\"{$formName}-{$this->name}-list\">"
;
137
138
foreach
($options as $index => $option) {
139
// associative arrays have index as value
140
if
(is_int($index)) {
141
$htmlList .=
"<option value=\"{$option}\">"
;
142
}
else
{
143
$htmlList .=
"<option value=\"{$index}\" label=\"{$option}\">"
;
144
}
145
}
146
147
$htmlList .=
"</datalist>"
;
148
}
else
{
149
$htmlList =
""
;
150
}
151
152
return
$htmlList;
153
}
154
160
protected
function
htmlInputAttributes
(): string
161
{
162
$attributes = parent::htmlInputAttributes();
163
164
if
($this->maxlength) {
165
$attributes .=
' maxlength="'
. $this->
htmlEscape
((
string
) $this->maxlength) .
'"'
;
166
}
167
if
($this->placeholder) {
168
$attributes .=
' placeholder="'
. $this->
htmlEscape
((
string
) $this->placeholder) .
'"'
;
169
}
170
if
($this->list) {
171
$attributes .=
" list=\"{$this->formName}-{$this->name}-list\""
;
172
}
173
174
$attributes .= $this->validator->getPatternAttribute();
175
176
return
$attributes;
177
}
178
184
protected
function
typeCastValue
(): void
185
{
186
// strip control characters
187
$this->value = preg_replace(
'/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u'
,
''
, (
string
) $this->value);
188
189
if
($this->normalize && class_exists(
"\\Normalizer"
)) {
190
$this->value = \Normalizer::normalize($this->value);
191
}
192
}
193
}
194
195
/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
Depage\HtmlForm\Abstracts\Element\$name
$name
Element name.
Definition
Element.php:36
Depage\HtmlForm\Abstracts\Element\htmlEscape
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition
Element.php:263
Depage\HtmlForm\Abstracts\Input
input element base class
Definition
Input.php:22
Depage\HtmlForm\Abstracts\Input\$value
$value
Input elements's value.
Definition
Input.php:124
Depage\HtmlForm\Abstracts\Input\$formName
$formName
Name of the parent HTML form.
Definition
Input.php:119
Depage\HtmlForm\Abstracts\Input\$label
$label
Input element - HTML label.
Definition
Input.php:37
Depage\HtmlForm\Abstracts\Input\$errorMessage
$errorMessage
Message that gets displayed in case of invalid input.
Definition
Input.php:218
Depage\HtmlForm\Abstracts\Input\htmlHelpMessage
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition
Input.php:685
Depage\HtmlForm\Abstracts\Input\htmlWrapperAttributes
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition
Input.php:636
Depage\HtmlForm\Abstracts\Input\htmlMarker
htmlMarker()
Returns elements' required-indicator.
Definition
Input.php:566
Depage\HtmlForm\Abstracts\Input\$type
$type
Input element type - HTML input type attribute.
Definition
Input.php:26
Depage\HtmlForm\Abstracts\Input\$helpMessage
$helpMessage
Extra help message.
Definition
Input.php:230
Depage\HtmlForm\Abstracts\Input\htmlValue
htmlValue()
Returns HTML-rendered element value.
Definition
Input.php:656
Depage\HtmlForm\Abstracts\Input\$marker
$marker
Input element - HTML marker text that marks required fields.
Definition
Input.php:92
Depage\HtmlForm\Abstracts\Input\htmlErrorMessage
htmlErrorMessage()
Returns HTML-rendered error message.
Definition
Input.php:666
Depage\HtmlForm\Elements\Text
HTML text input type.
Definition
Text.php:40
Depage\HtmlForm\Elements\Text\htmlInputAttributes
htmlInputAttributes()
renders text element specific HTML attributes
Definition
Text.php:160
Depage\HtmlForm\Elements\Text\$normalize
$normalize
wether to normalize unicode strings or not
Definition
Text.php:49
Depage\HtmlForm\Elements\Text\$list
$list
HTML list.
Definition
Text.php:59
Depage\HtmlForm\Elements\Text\__construct
__construct($name, $parameters, $form)
text class constructor
Definition
Text.php:69
Depage\HtmlForm\Elements\Text\typeCastValue
typeCastValue()
Converts value to element specific type.
Definition
Text.php:184
Depage\HtmlForm\Elements\Text\__toString
__toString()
Renders element to HTML.
Definition
Text.php:101
Depage\HtmlForm\Elements\Text\$placeholder
$placeholder
HTML placeholder attribute.
Definition
Text.php:44
Depage\HtmlForm\Elements\Text\htmlList
htmlList(?array $options=null, array|string|null $value=null)
Renders HTML datalist.
Definition
Text.php:130
Depage\HtmlForm\Elements\Text\setDefaults
setDefaults()
collects initial values across subclasses
Definition
Text.php:85
Depage\HtmlForm\Elements\Text\$maxlength
$maxlength
HTML maxlength attribute.
Definition
Text.php:54
Elements
Text.php
Generated on
for depage-forms by
doxygen
1.18.0
For an overview of all docs, go to
docs.depage.net