ILIAS  trunk Revision v11.0_alpha-1744-gb0451eebef4
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator 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) {
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): self
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 }
This implements commonalities between inputs.
Definition: Input.php:42
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Input.php:101
This implements the radio input.
Definition: Radio.php:34
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
getValue()
Get the value that is displayed in the input client side.
Definition: Input.php:89
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
withOption(string $value, string $label, ?string $byline=null)
Definition: Radio.php:72
This describes inputs that can be used in forms.
Definition: FormInput.php:32
withInput(InputData $input)
Get an input like this with input from post data.static
Definition: Radio.php:102