ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Radio.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
28use LogicException;
29use Closure;
30
35{
37 use Triggerer;
39
43 protected array $bylines = [];
44
48 protected function isClientSideValueOk($value): bool
49 {
50 return ($value === null || array_key_exists($value, $this->getOptions()));
51 }
52
57 {
58 if ($this->requirement_constraint !== null) {
59 return $this->requirement_constraint;
60 }
61 return $this->refinery->logical()->not($this->refinery->null())
62 ->withProblemBuilder(
63 fn($txt, $value) => $txt('required')
64 );
65 }
66
70 public function withOption(string $value, string $label, ?string $byline = null): C\Input\Field\Radio
71 {
72 $clone = clone $this;
73 $clone->options[$value] = $label;
74 if (!is_null($byline)) {
75 $clone->bylines[$value] = $byline;
76 }
77 return $clone;
78 }
79
80
81 public function getBylineFor(string $value): ?string
82 {
83 if (!array_key_exists($value, $this->bylines)) {
84 return null;
85 }
86 return $this->bylines[$value];
87 }
88
92 public function withInput(InputData $input): self
93 {
94 if ($this->getName() === null) {
95 throw new LogicException("Can only collect if input has a name.");
96 }
97 if (!$this->isDisabled()) {
98 $value = $input->getOr($this->getName(), null);
99 $clone = $this->withValue($value);
100 } else {
101 $value = $this->getValue();
102 $clone = $this;
103 }
104 $clone->content = $this->applyOperationsTo($value);
105 if ($clone->content->isError()) {
106 return $clone->withError("" . $clone->content->error());
107 }
108
109 $clone->content = $this->applyOperationsTo($value);
110
111 if ($clone->getError()) {
112 $clone->content = $clone->data_factory->error($clone->getError());
113 }
114
115 return $clone;
116 }
117
121 public function getUpdateOnLoadCode(): Closure
122 {
123 return fn($id) => "$('#$id').on('input', function(event) {
124 il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());
125 });
126 il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());";
127 }
128}
$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:70
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Radio.php:121
This implements commonalities between inputs.
Definition: Input.php:43
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.