ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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
4 docs/LICENSE */
5 
7 
9 use ILIAS\UI\Component as C;
14 
18 class Checkbox extends Input implements C\Input\Field\Checkbox, C\Changeable, C\Onloadable
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  if (!is_bool($value)) {
51  throw new \InvalidArgumentException(
52  "Unknown value type for checkbox: " . gettype($value)
53  );
54  }
55 
56  return parent::withValue($value);
57  }
58 
59 
63  public function withInput(InputData $post_input)
64  {
65  if ($this->getName() === null) {
66  throw new \LogicException("Can only collect if input has a name.");
67  }
68 
69  if (!$this->isDisabled()) {
70  $value = $post_input->getOr($this->getName(), "");
71  $clone = $this->withValue($value === "checked");
72  } else {
73  $value = $this->getValue();
74  $clone = $this;
75  }
76 
77  $clone->content = $this->applyOperationsTo($clone->getValue());
78  if ($clone->content->isError()) {
79  return $clone->withError("" . $clone->content->error());
80  }
81 
82  return $clone;
83  }
84 
88  public function appendOnLoad(C\Signal $signal)
89  {
90  return $this->appendTriggeredSignal($signal, 'load');
91  }
92 
96  public function withOnChange(C\Signal $signal)
97  {
98  return $this->withTriggeredSignal($signal, 'change');
99  }
100 
104  public function appendOnChange(C\Signal $signal)
105  {
106  return $this->appendTriggeredSignal($signal, 'change');
107  }
108 
112  public function withOnLoad(C\Signal $signal)
113  {
114  return $this->withTriggeredSignal($signal, 'load');
115  }
116 
120  public function getUpdateOnLoadCode() : \Closure
121  {
122  return function ($id) {
123  $code = "$('#$id').on('input', function(event) {
124  il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());
125  });
126  il.UI.input.onFieldUpdate(event, '$id', $('#$id').prop('checked').toString());";
127  return $code;
128  };
129  }
130 }
isDisabled()
Is this input disabled?
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event, &#39;$id&#39;, string_value);initially "onload" andon every input change. It must pass a readable string representation of its value in parameter &#39;string_value&#39;.string
Definition: Checkbox.php:120
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:12
This describes checkbox inputs.
Definition: Checkbox.php:13
appendTriggeredSignal(Component\Signal $signal, $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:31
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Checkbox.php:48
withTriggeredSignal(Component\Signal $signal, $event)
Add a triggered signal, replacing any other signals registered on the same event. ...
Definition: Triggerer.php:48
getValue()
Get the value that is displayed in the input client side.