ILIAS  release_8 Revision v8.24
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 === '' || array_key_exists($value, $this->getOptions()));
55 }
56
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}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This is a legacy support of Implementation\Component\Input\Field\Input that has been moved to Impleme...
Definition: Input.php:32
This implements the radio input.
Definition: Radio.php:35
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,...
Definition: Radio.php:132
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:32
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.
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.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.