text.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
12 use depage\htmlform\abstracts;
13 
38 class text extends abstracts\input
39 {
43  protected $placeholder;
44 
53  public function __construct($name, $parameters, $form)
54  {
55  parent::__construct($name, $parameters, $form);
56 
57  $this->list = (isset($parameters['list']) && is_array($parameters['list'])) ? $parameters['list'] : false;
58  }
59 
69  protected function setDefaults()
70  {
71  parent::setDefaults();
72 
73  // textClass elements have values of type string
74  $this->defaults['defaultValue'] = '';
75  $this->defaults['placeholder'] = false;
76  $this->defaults['maxlength'] = false;
77  }
78 
84  public function __toString()
85  {
86  $value = $this->htmlValue();
87  $inputAttributes = $this->htmlInputAttributes();
88  $marker = $this->htmlMarker();
89  $label = $this->htmlLabel();
90  $list = $this->htmlList();
91  $wrapperAttributes = $this->htmlWrapperAttributes();
93  $helpMessage = $this->htmlHelpMessage();
94 
95  return "<p {$wrapperAttributes}>" .
96  "<label>" .
97  "<span class=\"depage-label\">{$label}{$marker}</span>" .
98  "<input name=\"{$this->name}\" type=\"{$this->type}\"{$inputAttributes} value=\"{$value}\">" .
99  $list .
100  "</label>" .
101  $errorMessage .
102  $helpMessage .
103  "</p>\n";
104  }
105 
112  protected function htmlList($options = null)
113  {
114  if ($this->list && is_array($this->list)) {
115  $formName = $this->htmlFormName();
116  $options = $this->htmlEscape($this->list);
117 
118  $htmlList = "<datalist id=\"{$formName}-{$this->name}-list\">";
119 
120  foreach ($options as $index => $option) {
121  // associative arrays have index as value
122  if (is_int($index)) {
123  $htmlList .= "<option value=\"{$option}\">";
124  } else {
125  $htmlList .= "<option value=\"{$index}\" label=\"{$option}\">";
126  }
127  }
128 
129  $htmlList .= "</datalist>";
130  } else {
131  $htmlList = "";
132  }
133 
134  return $htmlList;
135  }
136 
142  protected function htmlInputAttributes()
143  {
144  $attributes = parent::htmlInputAttributes();
145 
146  if ($this->maxlength) $attributes .= " maxlength=\"{$this->maxlength}\"";
147  if ($this->placeholder) $attributes .= " placeholder=\"{$this->placeholder}\"";
148  if ($this->list) $attributes .= " list=\"{$this->formName}-{$this->name}-list\"";
149 
150  $attributes .= $this->validator->getPatternAttribute();
151 
152  return $attributes;
153  }
154 
160  protected function typeCastValue()
161  {
162  $this->value = (string) $this->value;
163  }
164 }
165 
166 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
input element base class
Definition: input.php:20
__construct($name, $parameters, $form)
text class constructor
Definition: text.php:53
$formName
Name of the parent HTML form. Used to identify the element once it&#39;s rendered.
Definition: input.php:74
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
getPatternAttribute()
returns validators&#39; regular expression as HTML5 pattern attribute
Definition: validator.php:102
HTML text input type.
Definition: text.php:38
setDefaults()
collects initial values across subclasses
Definition: text.php:69
htmlValue()
Returns HTML-rendered element value.
Definition: input.php:504
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition: input.php:533
$value
Input elements&#39;s value.
Definition: input.php:79
$label
Input element - HTML label.
Definition: input.php:36
Adds a placeholder for a form element.
Definition: placeholder.php:34
__toString()
Renders element to HTML.
Definition: text.php:84
htmlInputAttributes()
renders text element specific HTML attributes
Definition: text.php:142
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
$placeholder
HTML placeholder attribute.
Definition: text.php:43
htmlList($options=null)
Renders HTML datalist.
Definition: text.php:112
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
typeCastValue()
Converts value to element specific type.
Definition: text.php:160
parent validator class
Definition: validator.php:14