input.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\abstracts;
11 
12 use depage\htmlform\validators;
13 
20 abstract class input extends element
21 {
25  protected $type;
26 
36  protected $label;
37 
47  protected $marker;
48 
58  protected $required;
59 
69  protected $disabled;
70 
74  protected $formName;
75 
79  protected $value = null;
80 
96  protected $validator;
97 
107  protected $class;
108 
112  protected $classes;
113 
124  protected $autofocus = false;
125 
135  protected $autocapitalize;
136 
147  protected $autocorrect;
157  protected $autocomplete;
158 
162  protected $pattern;
163 
173  protected $errorMessage;
174 
185  protected $helpMessage;
186 
197  protected $helpMessageHtml;
198 
207  public function __construct($name, $parameters, $form)
208  {
209  $this->type = strtolower(str_replace('depage\\htmlform\\elements\\', '', get_class($this)));
210  $this->formName = $form->getName();
211 
212  parent::__construct($name, $parameters, $form);
213 
214  $this->validator = (isset($parameters['validator']))
215  ? validators\validator::factory($parameters['validator'], $this->log)
216  : validators\validator::factory($this->type, $this->log);
217  }
218 
228  protected function setDefaults()
229  {
230  parent::setDefaults();
231 
232  $this->defaults['autocapitalize'] = null;
233  $this->defaults['autocomplete'] = null;
234  $this->defaults['autocorrect'] = null;
235  $this->defaults['autofocus'] = false;
236  $this->defaults['disabled'] = false;
237  $this->defaults['errorMessage'] = _('Please enter valid data');
238  $this->defaults['label'] = $this->name;
239  $this->defaults['marker'] = '*';
240  $this->defaults['required'] = false;
241  $this->defaults['title'] = false;
242  $this->defaults['class'] = '';
243  $this->defaults['helpMessage'] = '';
244  $this->defaults['helpMessageHtml'] = '';
245  }
246 
255  public function validate()
256  {
257  if (!$this->validated) {
258  $this->validated = true;
259 
260  $this->valid = (
261  ($this->value !== null)
262  && ($this->validatorCall() || $this->isEmpty())
263  && (!$this->isEmpty() || !$this->required)
264  );
265  }
266 
267  return $this->valid;
268  }
269 
277  protected function validatorCall()
278  {
279  return $this->validator->validate($this->value);
280  }
281 
290  public function isEmpty()
291  {
292  return (
293  empty($this->value)
294  && $this->value !== '0'
295  && $this->value !== false
296  );
297  }
298 
310  public function setValue($newValue)
311  {
312  $this->value = $newValue;
313  $this->typeCastValue();
314  $this->validated = false;
315 
316  return $this->value;
317  }
318 
324  public function getValue()
325  {
326  return $this->value;
327  }
328 
334  public function getLabel()
335  {
336  return $this->label;
337  }
338 
344  public function clearValue()
345  {
346  $this->value = null;
347  }
348 
360  public function setDefaultValue($newDefaultValue)
361  {
362  $this->defaultValue = $newDefaultValue;
363  }
364 
373  protected function typeCastValue() {}
374 
381  public function setAutofocus($autofocus = true)
382  {
383  $this->autofocus = (bool) $autofocus;
384  }
385 
392  public function setRequired($required = true)
393  {
394  $this->required = (bool) $required;
395  $this->validated = false;
396  }
397 
404  public function setDisabled($disabled = true)
405  {
406  $this->disabled = (bool) $disabled;
407  }
408 
414  protected function htmlClasses()
415  {
416  $classes = 'input-' . $this->htmlEscape($this->type);
417 
418  if ($this->required) {
419  $classes .= ' required';
420  }
421  if ($this->disabled) {
422  $classes .= ' disabled';
423  }
424  if (($this->value !== null) && (!$this->validate())) {
425  $classes .= ' error';
426  }
427  if (isset($this->skin)) {
428  $classes .= ' skin-' . $this->htmlEscape($this->skin);
429  }
430  if (!empty($this->class)) {
431  $classes .= ' ' . $this->htmlEscape($this->class);
432  }
433 
434  return $classes;
435  }
436 
445  protected function htmlMarker()
446  {
447  return ($this->required) ? " <em>" . $this->htmlEscape($this->marker) . "</em>" : "";
448  }
449 
455  protected function htmlInputAttributes()
456  {
457  $attributes = '';
458 
459  if ($this->required) $attributes .= ' required="required"';
460  if ($this->disabled) $attributes .= ' disabled="disabled"';
461  if ($this->autofocus) $attributes .= ' autofocus="autofocus"';
462 
463  $autoAttributes = array(
464  "autocapitalize",
465  "autocomplete",
466  "autocorrect",
467  );
468  foreach ($autoAttributes as $attr) {
469  if (!is_null($this->$attr)) {
470  if ($this->$attr) {
471  $attributes .= " $attr=\"on\"";
472  } else {
473  $attributes .= " $attr=\"off\"";
474  }
475  }
476  }
477 
478  return $attributes;
479  }
480 
486  protected function htmlWrapperAttributes()
487  {
488  $attributes = "id=\"{$this->formName}-{$this->name}\" ";
489 
490  $attributes .= "class=\"" . $this->htmlClasses() . "\"";
491 
492  $attributes .= ($this->title) ? " title=\"" . $this->htmlEscape($this->title) . "\"" : "";
493 
494  $attributes .= " data-errorMessage=\"" . $this->htmlEscape($this->errorMessage) . "\"";
495 
496  return $attributes;
497  }
498 
504  protected function htmlValue()
505  {
506  return $this->htmlEscape($this->value === null ? $this->defaultValue : $this->value);
507  }
508 
514  protected function htmlErrorMessage()
515  {
516  if (!$this->valid
517  && $this->value !== null
518  && $this->errorMessage !== ""
519  ) {
520  $errorMessage = "<span class=\"errorMessage\">" . $this->htmlEscape($this->errorMessage) . "</span>";
521  } else {
522  $errorMessage = "";
523  }
524 
525  return $errorMessage;
526  }
527 
533  protected function htmlHelpMessage()
534  {
535  $helpMessage = '';
536  if (isset($this->helpMessage) && !empty($this->helpMessage)) {
537  // escaped message
538  $helpMessage = "<span class=\"helpMessage\">" . $this->htmlEscape($this->helpMessage) . "</span>";
539  }
540  if (isset($this->helpMessageHtml) && !empty($this->helpMessageHtml)) {
541  // html message
542  $helpMessage = "<span class=\"helpMessage\">" . $this->helpMessageHtml. "</span>";
543  }
544 
545  return $helpMessage;
546  }
547 }
548 
549 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
typeCastValue()
converts element value
Definition: input.php:373
setDisabled($disabled=true)
Sets the HTML disabled-attribute of the current input element.
Definition: input.php:404
__construct($name, $parameters, $form)
input class constructor
Definition: input.php:207
$classes
HTML classes attribute for rendering the input element.
Definition: input.php:112
$validator
Holds validator object reference.
Definition: input.php:96
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition: element.php:193
validatorCall()
custom validator call hook
Definition: input.php:277
$autocorrect
HTML autocorrect attribute.
Definition: input.php:147
validate()
Validates input element.
Definition: input.php:255
$required
True if the input element is required to hold a value to be valid.
Definition: input.php:58
input element base class
Definition: input.php:20
clearValue()
resets the value to null
Definition: input.php:344
setAutofocus($autofocus=true)
Sets the HTML autofocus-attribute of the current input element.
Definition: input.php:381
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition: input.php:455
setDefaultValue($newDefaultValue)
set the initial input element value
Definition: input.php:360
setValue($newValue)
set the input element value
Definition: input.php:310
$formName
Name of the parent HTML form. Used to identify the element once it&#39;s rendered.
Definition: input.php:74
getValue()
Returns the current input elements&#39; value.
Definition: input.php:324
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
$disabled
wether a input element will be disabled
Definition: input.php:69
$autocapitalize
HTML autocapitalize attribute.
Definition: input.php:135
htmlMarker()
Returns elements&#39; required-indicator.
Definition: input.php:445
isEmpty()
says wether the element value is empty
Definition: input.php:290
htmlErrorMessage()
Returns HTML-rendered error message.
Definition: input.php:514
getLabel()
Returns the current input elements&#39; label.
Definition: input.php:334
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
$autocomplete
HTML autocomplete attribute.
Definition: input.php:157
log($argument, $type=null)
error &amp; warning logger
Definition: element.php:168
validate($value, $parameters=array())
default validator.
Definition: validator.php:76
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
setRequired($required=true)
Sets the HTML required-attribute of the current input element.
Definition: input.php:392
$valid
Contains element validation status/result.
Definition: element.php:29
$type
Input element type - HTML input type attribute.
Definition: input.php:25
$pattern
HTML pattern attribute.
Definition: input.php:162
$class
class for paragraph
Definition: input.php:107
$autofocus
HTML autofocus attribute.
Definition: input.php:124
$helpMessage
Extra help message.
Definition: input.php:185
behold: the ├╝ber-class
Definition: element.php:20
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
setDefaults()
Sets the default values for input elements.
Definition: input.php:228
htmlClasses()
Returns string of the elements&#39; HTML-classes, separated by spaces.
Definition: input.php:414
parent validator class
Definition: validator.php:14
static factory($argument, $log=null)
valdiator object factory
Definition: validator.php:44
$helpMessageHtml
Extra hep message in html format.
Definition: input.php:197