depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Text.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
39class 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();
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>" .
121 "</p>\n";
122 }
123
130 protected function htmlList($options = null)
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->maxlength}\"";
166 }
167 if ($this->placeholder) {
168 $attributes .= " placeholder=\"{$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 : */
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
input element base class
Definition Input.php:22
$value
Input elements's value.
Definition Input.php:124
$formName
Name of the parent HTML form.
Definition Input.php:119
$label
Input element - HTML label.
Definition Input.php:37
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
$type
Input element type - HTML input type attribute.
Definition Input.php:26
$helpMessage
Extra help message.
Definition Input.php:230
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:656
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
HTML text input type.
Definition Text.php:40
htmlInputAttributes()
renders text element specific HTML attributes
Definition Text.php:160
$normalize
wether to normalize unicode strings or not
Definition Text.php:49
__construct($name, $parameters, $form)
text class constructor
Definition Text.php:69
typeCastValue()
Converts value to element specific type.
Definition Text.php:184
__toString()
Renders element to HTML.
Definition Text.php:101
$placeholder
HTML placeholder attribute.
Definition Text.php:44
setDefaults()
collects initial values across subclasses
Definition Text.php:85
htmlList($options=null)
Renders HTML datalist.
Definition Text.php:130
$maxlength
HTML maxlength attribute.
Definition Text.php:54