ILIAS  release_8 Revision v8.19-1-g4e8f2f9140c
All Data Structures Namespaces Files Functions Variables Modules Pages
Radio.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 LogicException;
29 use Closure;
30 
34 class Radio extends FormInput implements C\Input\Field\Radio
35 {
37  use Triggerer;
38 
42  protected array $options = [];
43 
47  protected array $bylines = [];
48 
52  protected function isClientSideValueOk($value): bool
53  {
54  return ($value === '' || array_key_exists($value, $this->getOptions()));
55  }
56 
60  protected function getConstraintForRequirement(): ?Constraint
61  {
62  if ($this->requirement_constraint !== null) {
63  return $this->requirement_constraint;
64  }
65 
66  return null;
67  }
68 
72  public function withOption(string $value, string $label, string $byline = null): C\Input\Field\Radio
73  {
74  $clone = clone $this;
75  $clone->options[$value] = $label;
76  if (!is_null($byline)) {
77  $clone->bylines[$value] = $byline;
78  }
79  return $clone;
80  }
81 
85  public function getOptions(): array
86  {
87  return $this->options;
88  }
89 
90 
91  public function getBylineFor(string $value): ?string
92  {
93  if (!array_key_exists($value, $this->bylines)) {
94  return null;
95  }
96  return $this->bylines[$value];
97  }
98 
102  public function withInput(InputData $input): C\Input\Field\Input
103  {
104  if ($this->getName() === null) {
105  throw new LogicException("Can only collect if input has a name.");
106  }
107  if (!$this->isDisabled()) {
108  $value = $input->getOr($this->getName(), "");
109  $clone = $this->withValue($value);
110  } else {
111  $value = $this->getValue();
112  $clone = $this;
113  }
114 
115  $clone->content = $this->applyOperationsTo($value);
116  if ($clone->content->isError()) {
117  return $clone->withError("" . $clone->content->error());
118  }
119 
120  $clone->content = $this->applyOperationsTo($value);
121 
122  if ($clone->getError()) {
123  $clone->content = $clone->data_factory->error($clone->getError());
124  }
125 
126  return $clone;
127  }
128 
132  public function getUpdateOnLoadCode(): Closure
133  {
134  return fn ($id) => "$('#$id').on('input', function(event) {
135  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());
136  });
137  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());";
138  }
139 }
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
This implements the radio input.
Definition: Radio.php:34
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
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.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This describes inputs that can be used in forms.
Definition: FormInput.php:31
withOption(string $value, string $label, string $byline=null)
Definition: Radio.php:72
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;.
Definition: Radio.php:132