ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Radio.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use LogicException;
29use Closure;
30
34class 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
61 {
62 if ($this->requirement_constraint !== null) {
63 return $this->requirement_constraint;
64 }
65 return $this->refinery->logical()->not($this->refinery->null())
66 ->withProblemBuilder(
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}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This implements the radio input.
Definition: Radio.php:35
withOption(string $value, string $label, ?string $byline=null)
Definition: Radio.php:74
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Radio.php:133
This implements commonalities between inputs.
Definition: Input.php:43
$txt
Definition: error.php:31
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
getOr(string $name, $default)
Get a named value from the data and fallback to default if that name does not exist.
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.