ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 === null || array_key_exists($value, $this->getOptions()));
55  }
56 
60  protected function getConstraintForRequirement(): ?Constraint
61  {
62  if ($this->requirement_constraint !== null) {
64  }
65  return $this->refinery->logical()->not($this->refinery->null())
67  fn($txt, $value) => $txt('required')
68  );
69  }
70 
74  public function withOption(string $value, string $label, ?string $byline = null): C\Input\Field\Radio
75  {
76  $clone = clone $this;
77  $clone->options[$value] = $label;
78  if (!is_null($byline)) {
79  $clone->bylines[$value] = $byline;
80  }
81  return $clone;
82  }
83 
87  public function getOptions(): array
88  {
89  return $this->options;
90  }
91 
92 
93  public function getBylineFor(string $value): ?string
94  {
95  if (!array_key_exists($value, $this->bylines)) {
96  return null;
97  }
98  return $this->bylines[$value];
99  }
100 
104  public function withInput(InputData $input): self
105  {
106  if ($this->getName() === null) {
107  throw new LogicException("Can only collect if input has a name.");
108  }
109  if (!$this->isDisabled()) {
110  $value = $input->getOr($this->getName(), null);
111  $clone = $this->withValue($value);
112  } else {
113  $value = $this->getValue();
114  $clone = $this;
115  }
116  $clone->content = $this->applyOperationsTo($value);
117  if ($clone->content->isError()) {
118  return $clone->withError("" . $clone->content->error());
119  }
120 
121  $clone->content = $this->applyOperationsTo($value);
122 
123  if ($clone->getError()) {
124  $clone->content = $clone->data_factory->error($clone->getError());
125  }
126 
127  return $clone;
128  }
129 
133  public function getUpdateOnLoadCode(): Closure
134  {
135  return fn($id) => "$('#$id').on('input', function(event) {
136  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());
137  });
138  il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());";
139  }
140 }
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
$txt
Definition: error.php:31
withProblemBuilder(callable $builder)
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:74
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:104