Input.php
Go to the documentation of this file.
1<?php
11
13
20abstract class Input extends Element
21{
25 protected $type;
26
36 protected $label;
37
47 protected $labelHtml;
48
58 protected $title;
59
69 protected $lang;
70
80 protected $defaultValue;
81
91 protected $marker;
92
102 protected $required;
103
113 protected $readonly;
114
118 protected $formName;
119
123 protected $value = null;
124
140 protected $validator;
141
151 protected $class;
152
156 protected $classes;
157
168 protected $autofocus = false;
169
180
191 protected $autocorrect;
201 protected $autocomplete;
202
206 protected $pattern;
207
217 protected $errorMessage;
218
229 protected $helpMessage;
230
242
255 public $dataPath;
256
265 public function __construct($name, $parameters, $form)
266 {
267 $this->type = str_replace('Depage\\HtmlForm\\Elements\\', '', get_class($this));
268 $this->formName = $form->getName();
269
270 parent::__construct($name, $parameters, $form);
271
272 $this->validator = (isset($parameters['validator']))
273 ? Validators\Validator::factory($parameters['validator'], $this->log)
274 : Validators\Validator::factory($this->type, $this->log);
275 }
276
286 protected function setDefaults()
287 {
288 parent::setDefaults();
289
290 $this->defaults['autocapitalize'] = null;
291 $this->defaults['autocomplete'] = null;
292 $this->defaults['autocorrect'] = null;
293 $this->defaults['autofocus'] = false;
294 $this->defaults['errorMessage'] = _('Please enter valid data');
295 $this->defaults['label'] = $this->name;
296 $this->defaults['labelHtml'] = '';
297 $this->defaults['marker'] = '*';
298 $this->defaults['required'] = false;
299 $this->defaults['disabled'] = false;
300 $this->defaults['readonly'] = false;
301 $this->defaults['title'] = false;
302 $this->defaults['class'] = '';
303 $this->defaults['lang'] = '';
304 $this->defaults['helpMessage'] = '';
305 $this->defaults['helpMessageHtml'] = '';
306 $this->defaults['dataPath'] = null;
307 }
308
317 public function validate()
318 {
319 if (!$this->validated) {
320 $this->validated = true;
321
322 $this->valid = (
323 ($this->value !== null)
324 && ($this->validatorCall() || $this->isEmpty())
325 && (!$this->isEmpty() || !$this->required)
326 );
327 }
328
329 return $this->valid;
330 }
331
340 public function invalidate()
341 {
342 $this->validated = true;
343 $this->valid = false;
344
345 return $this;
346 }
347
355 protected function validatorCall()
356 {
357 return $this->validator->validate($this->value);
358 }
359
368 public function isEmpty()
369 {
370 return (
371 empty($this->value)
372 && $this->value !== '0'
373 && $this->value !== false
374 );
375 }
376
388 public function setValue($newValue)
389 {
390 $this->value = $newValue;
391 $this->typeCastValue();
392 $this->validated = false;
393
394 return $this->value;
395 }
396
402 public function getValue()
403 {
404 return $this->value;
405 }
406
410 public function setLabel($label)
411 {
412 $this->label = $label;
413
414 return $this;
415 }
421 public function getLabel()
422 {
423 return $this->label;
424 }
425
432 public function setErrorMessage($message)
433 {
434 $this->errorMessage = $message;
435
436 return $this;
437 }
444 public function getErrorMessage()
445 {
446 return $this->errorMessage;
447 }
448
454 public function clearValue()
455 {
456 $this->value = null;
457
458 return $this->value;
459 }
460
472 public function setDefaultValue($newDefaultValue)
473 {
474 $this->defaultValue = $newDefaultValue;
475
476 return $this;
477 }
478
484 public function getDefaultValue()
485 {
486 return $this->defaultValue;
487 }
488
497 protected function typeCastValue() {}
498
505 public function setAutofocus($autofocus = true)
506 {
507 $this->autofocus = (bool) $autofocus;
508
509 return $this;
510 }
511
518 public function setRequired($required = true)
519 {
520 $this->required = (bool) $required;
521 $this->validated = false;
522
523 return $this;
524 }
525
531 protected function htmlClasses()
532 {
533 $classes = 'input-' . $this->htmlEscape(strtolower($this->type));
534
535 if ($this->required) {
536 $classes .= ' required';
537 }
538 if ($this->disabled) {
539 $classes .= ' disabled';
540 }
541 if ($this->readonly) {
542 $classes .= ' readonly';
543 }
544 if (($this->value !== null) && (!$this->validate())) {
545 $classes .= ' error';
546 }
547 if (isset($this->skin)) {
548 $classes .= ' skin-' . $this->htmlEscape($this->skin);
549 }
550 if (!empty($this->class)) {
551 $classes .= ' ' . $this->htmlEscape($this->class);
552 }
553
554 return $classes;
555 }
556
565 protected function htmlMarker()
566 {
567 return ($this->required) ? " <em>" . $this->htmlEscape($this->marker) . "</em>" : "";
568 }
569
575 protected function htmlInputAttributes()
576 {
577 $attributes = '';
578
579 if ($this->required) $attributes .= ' required="required"';
580 if ($this->disabled) $attributes .= ' disabled="disabled"';
581 if ($this->readonly) $attributes .= ' readonly="readonly"';
582 if ($this->autofocus) $attributes .= ' autofocus="autofocus"';
583
584 $autoAttributes = array(
585 "autocapitalize",
586 "autocorrect",
587 );
588 foreach ($autoAttributes as $attr) {
589 if (!is_null($this->$attr)) {
590 if ($this->$attr) {
591 $attributes .= " $attr=\"on\"";
592 } else {
593 $attributes .= " $attr=\"off\"";
594 }
595 }
596 }
597 // enable autocomplete attribute according to
598 // https://html.spec.whatwg.org/multipage/forms.html#autofill
599 if (!is_null($this->autocomplete)) {
600 if (is_string($this->autocomplete)) {
601 $attributes .= " autocomplete=\"" . $this->htmlEscape($this->autocomplete) . "\"";
602 } else if ($this->autocomplete === true) {
603 $attributes .= " autocomplete=\"on\"";
604 } else {
605 $attributes .= " autocomplete=\"off\"";
606 }
607 }
608
609 return $attributes;
610 }
611
615 protected function htmlDataAttributes()
616 {
617 $this->dataAttr['errorMessage'] = $this->errorMessage;
618
619 return parent::htmlDataAttributes();
620 }
621
627 protected function htmlWrapperAttributes()
628 {
629 $attributes = "id=\"{$this->formName}-{$this->name}\" ";
630
631 $attributes .= "class=\"" . $this->htmlClasses() . "\"";
632
633 $attributes .= ($this->title) ? " title=\"" . $this->htmlEscape($this->title) . "\"" : "";
634
635 $attributes .= ($this->lang) ? " lang=\"" . $this->htmlEscape($this->lang) . "\"" : "";
636
637 $attributes .= $this->htmlDataAttributes();
638
639 return $attributes;
640 }
641
647 protected function htmlValue()
648 {
649 return $this->htmlEscape($this->value === null ? $this->defaultValue : $this->value);
650 }
651
657 protected function htmlErrorMessage()
658 {
659 if (!$this->valid
660 && $this->value !== null
661 && $this->errorMessage !== ""
662 ) {
663 $errorMessage = "<span class=\"errorMessage\">" . $this->htmlEscape($this->errorMessage) . "</span>";
664 } else {
665 $errorMessage = "";
666 }
667
668 return $errorMessage;
669 }
670
676 protected function htmlHelpMessage()
677 {
678 $helpMessage = '';
679 if (isset($this->helpMessage) && !empty($this->helpMessage)) {
680 // escaped message
681 $helpMessage = "<span class=\"helpMessage\">" . $this->htmlEscape($this->helpMessage) . "</span>";
682 }
683 if (isset($this->helpMessageHtml) && !empty($this->helpMessageHtml)) {
684 // html message
685 $helpMessage = "<span class=\"helpMessage\">" . $this->helpMessageHtml. "</span>";
686 }
687
688 return $helpMessage;
689 }
690}
691
692/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
behold: the über-class
Definition Element.php:21
$valid
Contains element validation status/result.
Definition Element.php:30
log($argument, $type=null)
error & warning logger
Definition Element.php:242
htmlEscape($options=array())
Escapes HTML in strings and arrays of strings.
Definition Element.php:267
input element base class
Definition Input.php:21
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Input.php:575
$dataPath
Extra information about the data that is saved inside the element.
Definition Input.php:255
$value
Input elements's value.
Definition Input.php:123
setRequired($required=true)
Sets the HTML required-attribute of the current input element.
Definition Input.php:518
$formName
Name of the parent HTML form.
Definition Input.php:118
getLabel()
Returns the current input elements' label.
Definition Input.php:421
$label
Input element - HTML label.
Definition Input.php:36
validate()
Validates input element.
Definition Input.php:317
setAutofocus($autofocus=true)
Sets the HTML autofocus-attribute of the current input element.
Definition Input.php:505
$autofocus
HTML autofocus attribute.
Definition Input.php:168
$class
class for paragraph
Definition Input.php:151
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:217
$helpMessageHtml
Extra help message in html format.
Definition Input.php:241
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:676
$required
True if the input element is required to hold a value to be valid.
Definition Input.php:102
__construct($name, $parameters, $form)
input class constructor
Definition Input.php:265
getErrorMessage()
getErrorMessage
Definition Input.php:444
clearValue()
resets the value to null
Definition Input.php:454
$classes
HTML classes attribute for rendering the input element.
Definition Input.php:156
$autocomplete
HTML autocomplete attribute.
Definition Input.php:201
typeCastValue()
converts element value
Definition Input.php:497
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Input.php:615
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:627
$lang
Input element - lang.
Definition Input.php:69
$autocapitalize
HTML autocapitalize attribute.
Definition Input.php:179
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:565
setErrorMessage($message)
setErrorMessage
Definition Input.php:432
setValue($newValue)
set the input element value
Definition Input.php:388
validatorCall()
custom validator call hook
Definition Input.php:355
$type
Input element type - HTML input type attribute.
Definition Input.php:25
$defaultValue
Input element - default value.
Definition Input.php:80
$readonly
wether a input element will be readonly
Definition Input.php:113
$validator
Holds validator object reference.
Definition Input.php:140
$helpMessage
Extra help message.
Definition Input.php:229
htmlValue()
Returns HTML-rendered element value.
Definition Input.php:647
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:91
$autocorrect
HTML autocorrect attribute.
Definition Input.php:191
setDefaultValue($newDefaultValue)
set the initial input element value
Definition Input.php:472
getValue()
Returns the current input elements' value.
Definition Input.php:402
htmlClasses()
Returns string of the elements' HTML-classes, separated by spaces.
Definition Input.php:531
setDefaults()
Sets the default values for input elements.
Definition Input.php:286
invalidate()
Invalidates input.
Definition Input.php:340
$title
Input element - HTML title.
Definition Input.php:58
isEmpty()
says wether the element value is empty
Definition Input.php:368
$labelHtml
Input element - HTML label in html format.
Definition Input.php:47
$pattern
HTML pattern attribute.
Definition Input.php:206
getDefaultValue()
gets the initial input element value
Definition Input.php:484
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:657
setLabel($label)
set the label of the input
Definition Input.php:410
static factory($argument, $log=null)
valdiator object factory
Definition Validator.php:44
Abstract element classes.
Definition Container.php:10
Validators for HTML input-elements.