depage-forms v1.4.1
html forms made easy
Loading...
Searching...
No Matches
Boolean.php
Go to the documentation of this file.
1<?php
2
10
11namespace Depage\HtmlForm\Elements;
12
13use Depage\HtmlForm\Abstracts;
14
50{
60 protected function setDefaults(): void
61 {
62 parent::setDefaults();
63 $this->defaults['defaultValue'] = false;
64 $this->defaults['errorMessage'] = _('Please check this box if you want to proceed');
65 }
66
72 public function __toString(): string
73 {
74 $inputAttributes = $this->htmlInputAttributes();
75 $label = $this->htmlLabel();
76 $marker = $this->htmlMarker();
77 $wrapperAttributes = $this->htmlWrapperAttributes();
80
81 $selected = '';
82 if (is_null($this->getValue()) && $this->defaultValue === true) {
83 $selected = " checked=\"yes\"";
84 } elseif ($this->getValue() === true) {
85 $selected = " checked=\"yes\"";
86 }
87
88 return "<p {$wrapperAttributes}>" .
89 "<label>" .
90 "<input type=\"checkbox\" name=\"{$this->name}\"{$inputAttributes} value=\"true\"{$selected}>" .
91 "<span class=\"depage-label\">{$label}{$marker}</span>" .
92 "</label>" .
95 "</p>\n";
96 }
97
107 public function validate(): bool
108 {
109 if (!$this->validated) {
110 $this->validated = true;
111
112 $this->valid = (
113 ($this->value !== null)
114 && ($this->validator->validate($this->value) || $this->isEmpty())
115 && ($this->value || !$this->required)
116 );
117 }
118
119 return $this->valid;
120 }
121
131 public function setValue(mixed $newValue): bool
132 {
133 if (is_bool($newValue)) {
134 $this->value = $newValue;
135 } elseif ($newValue === "true") {
136 $this->value = true;
137 } else {
138 $this->value = false;
139 }
140
141 return $this->value;
142 }
143}
144
145/* vim:set ft=php sw=4 sts=4 fdm=marker et : */
$valid
Contains element validation status/result.
Definition Element.php:39
input element base class
Definition Input.php:22
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition Input.php:576
$value
Input elements's value.
Definition Input.php:124
$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
$helpMessage
Extra help message.
Definition Input.php:230
$marker
Input element - HTML marker text that marks required fields.
Definition Input.php:92
getValue()
Returns the current input elements' value.
Definition Input.php:402
isEmpty()
says wether the element value is empty
Definition Input.php:368
htmlErrorMessage()
Returns HTML-rendered error message.
Definition Input.php:666
HTML single checkbox input type.
Definition Boolean.php:50
validate()
validates boolean input element value
Definition Boolean.php:107
setValue(mixed $newValue)
set the boolean element value
Definition Boolean.php:131
__toString()
Renders element to HTML.
Definition Boolean.php:72
setDefaults()
collects initial values across subclasses.
Definition Boolean.php:60