ILIAS  release_7 Revision v7.30-3-g800a261c036
Checkbox.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see
4docs/LICENSE */
5
7
8use ILIAS\Data\Factory as DataFactory;
14
19{
21 use Triggerer;
22
26 protected function getConstraintForRequirement()
27 {
28 return null;
29 }
30
34 protected function isClientSideValueOk($value) : bool
35 {
36 if ($value == "checked" || $value === "" || is_bool($value)) {
37 return true;
38 } else {
39 return false;
40 }
41 }
42
43
48 public function withValue($value)
49 {
50 $value = $value ?? false;
51
52 if (!is_bool($value)) {
53 throw new \InvalidArgumentException(
54 "Unknown value type for checkbox: " . gettype($value)
55 );
56 }
57
61 $clone = parent::withValue($value);
62 return $clone;
63 }
64
65
69 public function withInput(InputData $post_input)
70 {
71 if ($this->getName() === null) {
72 throw new \LogicException("Can only collect if input has a name.");
73 }
74
75 if (!$this->isDisabled()) {
76 $value = $post_input->getOr($this->getName(), "");
77 $clone = $this->withValue($value === "checked");
78 } else {
79 $value = $this->getValue();
80 $clone = $this;
81 }
82
83 $clone->content = $this->applyOperationsTo($clone->getValue());
84 if ($clone->content->isError()) {
85 return $clone->withError("" . $clone->content->error());
86 }
87
88 return $clone;
89 }
90
94 public function appendOnLoad(C\Signal $signal)
95 {
96 return $this->appendTriggeredSignal($signal, 'load');
97 }
98
102 public function withOnChange(C\Signal $signal)
103 {
104 return $this->withTriggeredSignal($signal, 'change');
105 }
106
110 public function appendOnChange(C\Signal $signal)
111 {
112 return $this->appendTriggeredSignal($signal, 'change');
113 }
114
118 public function withOnLoad(C\Signal $signal)
119 {
120 return $this->withTriggeredSignal($signal, 'load');
121 }
122
126 public function getUpdateOnLoadCode() : \Closure
127 {
128 return function ($id) {
129 $code = "$('#$id').on('input', function(event) {
130 il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());
131 });
132 il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());";
133 return $code;
134 };
135 }
136}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
This describes checkbox inputs.
Definition: Checkbox.php:14
isDisabled()
Is this input disabled?
This describes commonalities between all inputs.
Definition: Input.php:32
getValue()
Get the value that is displayed in the input client side.
withValue($value)
Get an input like this with another value displayed on the client side.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
withTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any other signals registered on the same event.
Definition: Triggerer.php:48
appendTriggeredSignal(Component\Signal $signal, $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:31
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.