depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Input.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Abstracts;
12
13use Depage\HtmlForm\Validators;
14
21abstract class Input extends Element
22{
26 protected $type;
27
37 protected $label;
38
48 protected $labelHtml;
49
59 protected $title;
60
70 protected $lang;
71
81 protected $defaultValue;
82
92 protected $marker;
93
103 protected $required;
104
114 protected $readonly;
115
119 protected $formName;
120
124 protected $value = null;
125
141 protected $validator;
142
152 protected $class;
153
157 protected $classes;
158
169 protected $autofocus = false;
170
181
191
192 protected $autocorrect;
202 protected $autocomplete;
203
207 protected $pattern;
208
218 protected $errorMessage;
219
230 protected $helpMessage;
231
243
256 public $dataPath;
257
266 public function __construct(string $name, array $parameters, object $form)
267 {
268 $this->type = str_replace('Depage\\HtmlForm\\Elements\\', '', get_class($this));
269 $this->formName = $form->getName();
270
271 parent::__construct($name, $parameters, $form);
272
273 $this->validator = (isset($parameters['validator']))
274 ? Validators\Validator::factory($parameters['validator'], $this->log)
275 : Validators\Validator::factory($this->type, $this->log);
276 }
277
287 protected function setDefaults(): void
288 {
289 parent::setDefaults();
290
291 $this->defaults['autocapitalize'] = null;
292 $this->defaults['autocomplete'] = null;
293 $this->defaults['autocorrect'] = null;
294 $this->defaults['autofocus'] = false;
295 $this->defaults['errorMessage'] = _('Please enter valid data');
296 $this->defaults['label'] = $this->name;
297 $this->defaults['labelHtml'] = '';
298 $this->defaults['marker'] = '*';
299 $this->defaults['required'] = 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(): bool
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(): self
341 {
342 $this->validated = true;
343 $this->valid = false;
344
345 return $this;
346 }
347
355 protected function validatorCall(): bool
356 {
357 return $this->validator->validate($this->value);
358 }
359
368 public function isEmpty(): bool
369 {
370 return (
371 empty($this->value)
372 && $this->value !== '0'
373 && $this->value !== false
374 );
375 }
376
388 public function setValue(mixed $newValue): mixed
389 {
390 $this->value = $newValue;
391 $this->typeCastValue();
392 $this->validated = false;
393
394 return $this->value;
395 }
396
402 public function getValue(): mixed
403 {
404 return $this->value;
405 }
406
410 public function setLabel(string $label): self
411 {
412 $this->label = $label;
413
414 return $this;
415 }
416
421 public function getLabel(): string
422 {
423 return $this->label;
424 }
425
432 public function setErrorMessage(string $message): self
433 {
434 $this->errorMessage = $message;
435
436 return $this;
437 }
438
444 public function getErrorMessage(): string
445 {
446 return $this->errorMessage;
447 }
448
454 public function clearValue(): void
455 {
456 $this->value = null;
457 }
458
470 public function setDefaultValue(mixed $newDefaultValue): self
471 {
472 $this->defaultValue = $newDefaultValue;
473
474 return $this;
475 }
476
482 public function getDefaultValue(): mixed
483 {
484 return $this->defaultValue;
485 }
486
495 protected function typeCastValue(): void
496 {
497 // to be overridden by child classes
498 }
499
506 public function setAutofocus(bool $autofocus = true): self
507 {
508 $this->autofocus = (bool) $autofocus;
509
510 return $this;
511 }
512
519 public function setRequired(bool $required = true): self
520 {
521 $this->required = (bool) $required;
522 $this->validated = false;
523
524 return $this;
525 }
526
532 protected function htmlClasses()
533 {
534 $classes = 'input-' . $this->htmlEscape(strtolower($this->type));
535
536 if ($this->required) {
537 $classes .= ' required';
538 }
539 if ($this->disabled) {
540 $classes .= ' disabled';
541 }
542 if ($this->readonly) {
543 $classes .= ' readonly';
544 }
545 if (($this->value !== null) && (!$this->validate())) {
546 $classes .= ' error';
547 }
548 if (isset($this->skin)) {
549 $classes .= ' skin-' . $this->htmlEscape($this->skin);
550 }
551 if (!empty($this->class)) {
552 $classes .= ' ' . $this->htmlEscape($this->class);
553 }
554
555 return $classes;
556 }
557
566 protected function htmlMarker()
567 {
568 return ($this->required) ? " <em>" . $this->htmlEscape($this->marker) . "</em>" : "";
569 }
570
576 protected function htmlInputAttributes(): string
577 {
578 $attributes = '';
579
580 if ($this->required) {
581 $attributes .= ' required="required"';
582 }
583 if ($this->disabled) {
584 $attributes .= ' disabled="disabled"';
585 }
586 if ($this->readonly) {
587 $attributes .= ' readonly="readonly"';
588 }
589 if ($this->autofocus) {
590 $attributes .= ' autofocus="autofocus"';
591 }
592
593 $autoAttributes = [
594 "autocapitalize",
595 "autocorrect",
596 ];
597 foreach ($autoAttributes as $attr) {
598 if (!is_null($this->$attr)) {
599 if ($this->$attr) {
600 $attributes .= " $attr=\"on\"";
601 } else {
602 $attributes .= " $attr=\"off\"";
603 }
604 }
605 }
606 // enable autocomplete attribute according to
607 // https://html.spec.whatwg.org/multipage/forms.html#autofill
608 if (!is_null($this->autocomplete)) {
609 if (is_string($this->autocomplete)) {
610 $attributes .= " autocomplete=\"" . $this->htmlEscape($this->autocomplete) . "\"";
611 } elseif ($this->autocomplete === true) {
612 $attributes .= " autocomplete=\"on\"";
613 } else {
614 $attributes .= " autocomplete=\"off\"";
615 }
616 }
617
618 return $attributes;
619 }
620
624 protected function htmlDataAttributes(): string
625 {
626 $this->dataAttr['errorMessage'] = $this->errorMessage;
627
628 return parent::htmlDataAttributes();
629 }
630
636 protected function htmlWrapperAttributes()
637 {
638 $attributes = "id=\"{$this->formName}-{$this->name}\" ";
639
640 $attributes .= "class=\"" . $this->htmlClasses() . "\"";
641
642 $attributes .= ($this->title) ? " title=\"" . $this->htmlEscape($this->title) . "\"" : "";
643
644 $attributes .= ($this->lang) ? " lang=\"" . $this->htmlEscape($this->lang) . "\"" : "";
645
646 $attributes .= $this->htmlDataAttributes();
647
648 return $attributes;
649 }
650
656 protected function htmlValue()
657 {
658 return $this->htmlEscape($this->value === null ? $this->defaultValue : $this->value);
659 }
660
666 protected function htmlErrorMessage()
667 {
668 if (!$this->valid
669 && $this->value !== null
670 && $this->errorMessage !== ""
671 ) {
672 $errorMessage = "<span class=\"errorMessage\">" . $this->htmlEscape($this->errorMessage) . "</span>";
673 } else {
674 $errorMessage = "";
675 }
676
677 return $errorMessage;
678 }
679
685 protected function htmlHelpMessage()
686 {
687 $helpMessage = '';
688 if (isset($this->helpMessage) && !empty($this->helpMessage)) {
689 // escaped message
690 $helpMessage = "<span class=\"helpMessage\">" . $this->htmlEscape($this->helpMessage) . "</span>";
691 }
692 if (isset($this->helpMessageHtml) && !empty($this->helpMessageHtml)) {
693 // html message
694 $helpMessage = "<span class=\"helpMessage\">" . $this->helpMessageHtml . "</span>";
695 }
696
697 return $helpMessage;
698 }
699}
700
701/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
behold: the über-class
Definition Element.php:30
$valid
Contains element validation status/result.
Definition Element.php:39
log(string $argument, string $type=null)
error & warning logger
Definition Element.php:236
htmlEscape(array|string $options=[])
Escapes HTML in strings and arrays of strings.
Definition Element.php:261
input element base class
Definition Input.php:22
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Input.php:576
$dataPath
Extra information about the data that is saved inside the element.
Definition Input.php:256
$value
Input elements's value.
Definition Input.php:124
$formName
Name of the parent HTML form.
Definition Input.php:119
getLabel()
Returns the current input elements' label.
Definition Input.php:421
$label
Input element - HTML label.
Definition Input.php:37
validate()
Validates input element.
Definition Input.php:317
$autofocus
HTML autofocus attribute.
Definition Input.php:169
setValue(mixed $newValue)
set the input element value
Definition Input.php:388
$class
class for paragraph
Definition Input.php:152
$errorMessage
Message that gets displayed in case of invalid input.
Definition Input.php:218
$helpMessageHtml
Extra help message in html format.
Definition Input.php:242
htmlHelpMessage()
Returns HTML-rendered helpMessage.
Definition Input.php:685
$required
True if the input element is required to hold a value to be valid.
Definition Input.php:103
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:157
$autocomplete
HTML autocomplete attribute.
Definition Input.php:202
typeCastValue()
converts element value
Definition Input.php:495
htmlDataAttributes()
Returns dataAttr escaped as attribute string.
Definition Input.php:624
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition Input.php:636
__construct(string $name, array $parameters, object $form)
input class constructor
Definition Input.php:266
$lang
Input element - lang.
Definition Input.php:70
$autocapitalize
HTML autocapitalize attribute.
Definition Input.php:180
htmlMarker()
Returns elements' required-indicator.
Definition Input.php:566
validatorCall()
custom validator call hook
Definition Input.php:355
$type
Input element type - HTML input type attribute.
Definition Input.php:26
$defaultValue
Input element - default value.
Definition Input.php:81
setErrorMessage(string $message)
setErrorMessage
Definition Input.php:432
$readonly
wether a input element will be readonly
Definition Input.php:114
$validator
Holds validator object reference.
Definition Input.php:141
$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
$autocorrect
HTML autocorrect attribute.
Definition Input.php:192
setAutofocus(bool $autofocus=true)
Sets the HTML autofocus-attribute of the current input element.
Definition Input.php:506
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:532
setDefaults()
Sets the default values for input elements.
Definition Input.php:287
setDefaultValue(mixed $newDefaultValue)
set the initial input element value
Definition Input.php:470
invalidate()
Invalidates input.
Definition Input.php:340
$title
Input element - HTML title.
Definition Input.php:59
setLabel(string $label)
set the label of the input
Definition Input.php:410
setRequired(bool $required=true)
Sets the HTML required-attribute of the current input element.
Definition Input.php:519
isEmpty()
says wether the element value is empty
Definition Input.php:368
$labelHtml
Input element - HTML label in html format.
Definition Input.php:48
$pattern
HTML pattern attribute.
Definition Input.php:207
getDefaultValue()
gets the initial input element value
Definition Input.php:482
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
static factory($argument, $log=null)
valdiator object factory
Definition Validator.php:45
Validators for HTML input-elements.