boolean.php
Go to the documentation of this file.
1 <?php
10 namespace depage\htmlform\elements;
11 
12 use depage\htmlform\abstracts;
13 
48 class boolean extends abstracts\input
49 {
59  protected function setDefaults()
60  {
61  parent::setDefaults();
62  $this->defaults['defaultValue'] = false;
63  $this->defaults['errorMessage'] = _('Please check this box if you want to proceed');
64  }
65 
71  public function __toString()
72  {
73  $inputAttributes = $this->htmlInputAttributes();
74  $label = $this->htmlLabel();
75  $marker = $this->htmlMarker();
76  $wrapperAttributes = $this->htmlWrapperAttributes();
78  $helpMessage = $this->htmlHelpMessage();
79 
80  $selected = ($this->htmlValue() === true) ? " checked=\"yes\"" : '';
81 
82  return "<p {$wrapperAttributes}>" .
83  "<label>" .
84  "<input type=\"checkbox\" name=\"{$this->name}\"{$inputAttributes} value=\"true\"{$selected}>" .
85  "<span class=\"depage-label\">{$label}{$marker}</span>" .
86  "</label>" .
88  $helpMessage .
89  "</p>\n";
90  }
91 
101  public function validate()
102  {
103  if (!$this->validated) {
104  $this->validated = true;
105 
106  $this->valid = (($this->value !== null)
107  && ($this->validator->validate($this->value) || $this->isEmpty())
108  && ($this->value || !$this->required)
109  );
110  }
111 
112  return $this->valid;
113  }
114 
124  public function setValue($newValue)
125  {
126  if (is_bool($newValue)) {
127  $this->value = $newValue;
128  } elseif ($newValue === "true") {
129  $this->value = true;
130  } else {
131  $this->value = false;
132  }
133 
134  return $this->value;
135  }
136 }
137 
138 /* vim:set ft=php sw=4 sts=4 fdm=marker et : */
HTML single checkbox input type.
Definition: boolean.php:48
input element base class
Definition: input.php:20
setValue($newValue)
set the boolean element value
Definition: boolean.php:124
htmlInputAttributes()
Returns string of HTML attributes for input element.
Definition: input.php:455
htmlWrapperAttributes()
Returns string of HTML attributes for element wrapper paragraph.
Definition: input.php:486
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
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
validate($value, $parameters=array())
default validator.
Definition: validator.php:76
__toString()
Renders element to HTML.
Definition: boolean.php:71
$errorMessage
Message that gets displayed in case of invalid input.
Definition: input.php:173
$valid
Contains element validation status/result.
Definition: element.php:29
$helpMessage
Extra help message.
Definition: input.php:185
$marker
Input element - HTML marker text that marks required fields.
Definition: input.php:47
parent validator class
Definition: validator.php:14
validate()
validates boolean input element value
Definition: boolean.php:101
setDefaults()
collects initial values across subclasses.
Definition: boolean.php:59