ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Checkbox.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use Closure;
29use LogicException;
30use InvalidArgumentException;
31
36{
38
43 {
44 if ($this->requirement_constraint !== null) {
45 return $this->requirement_constraint;
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}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This implements the checkbox input.
Definition: Checkbox.php:36
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Checkbox.php:141
withValue($value)
Get an input like this with another value displayed on the client side.static
Definition: Checkbox.php:67
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
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
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.