ILIAS  trunk Revision v11.0_alpha-1744-gb0451eebef4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Checkbox.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use ILIAS\UI\Component as C;
28 use Closure;
29 use LogicException;
31 
35 class Checkbox extends FormInput implements C\Input\Field\Checkbox, C\Changeable, C\Onloadable
36 {
38 
42  protected function getConstraintForRequirement(): ?Constraint
43  {
44  if ($this->requirement_constraint !== null) {
46  }
47 
48  return null;
49  }
50 
54  protected function isClientSideValueOk($value): bool
55  {
56  if ($value == "checked" || $value === "" || is_bool($value)) {
57  return true;
58  } else {
59  return false;
60  }
61  }
62 
63 
67  public function withValue($value): self
68  {
69  $value = $value ?? false;
70 
71  if (!is_bool($value)) {
72  throw new InvalidArgumentException(
73  "Unknown value type for checkbox: " . gettype($value)
74  );
75  }
76 
77  return parent::withValue($value);
78  }
79 
80 
84  public function withInput(InputData $input): self
85  {
86  if ($this->getName() === null) {
87  throw new LogicException("Can only collect if input has a name.");
88  }
89 
90  if (!$this->isDisabled()) {
91  $value = $input->getOr($this->getName(), "");
92  $clone = $this->withValue($value === "checked");
93  } else {
94  $clone = $this;
95  }
96 
97  $clone->content = $this->applyOperationsTo($clone->getValue());
98  if ($clone->content->isError()) {
100  return $clone->withError("" . $clone->content->error());
101  }
102 
103  return $clone;
104  }
105 
109  public function appendOnLoad(C\Signal $signal): C\Onloadable
110  {
111  return $this->appendTriggeredSignal($signal, 'load');
112  }
113 
117  public function withOnChange(C\Signal $signal): C\Changeable
118  {
119  return $this->withTriggeredSignal($signal, 'change');
120  }
121 
125  public function appendOnChange(C\Signal $signal): C\Changeable
126  {
127  return $this->appendTriggeredSignal($signal, 'change');
128  }
129 
133  public function withOnLoad(C\Signal $signal): C\Onloadable
134  {
135  return $this->withTriggeredSignal($signal, 'load');
136  }
137 
141  public function getUpdateOnLoadCode(): Closure
142  {
143  return fn($id) => "$('#$id').on('input', function(event) {
144  il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());
145  });
146  il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());";
147  }
148 }
appendTriggeredSignal(C\Signal $signal, string $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:47
withTriggeredSignal(C\Signal $signal, string $event)
Add a triggered signal, replacing any other signals registered on the same event. ...
Definition: Triggerer.php:62
This implements the checkbox input.
Definition: Checkbox.php:35
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withInput(InputData $input)
Get an input like this with input from post data.static
Definition: Checkbox.php:84
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This describes inputs that can be used in forms.
Definition: FormInput.php:32