ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
FormInput.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\Data\Factory as DataFactory;
26use ILIAS\Refinery\Factory as Refinery;
32use Generator;
33
34abstract class FormInput extends Input implements FormInputInternal
35{
37 use Triggerer;
38
39 protected bool $is_disabled = false;
40 protected bool $is_required = false;
42
43 public function __construct(
44 DataFactory $data_factory,
46 protected string $label,
47 protected ?string $byline = null,
48 ) {
49 parent::__construct($data_factory, $refinery);
50 }
51
55 public function withInput(InputData $input): self
56 {
57 if (!$this->isDisabled()) {
58 return parent::withInput($input);
59 } else {
60 $clone = $this;
61 $clone->content = $this->applyOperationsTo($clone->getValue());
62 if ($clone->content->isError()) {
63 $error = $clone->content->error();
64 if ($error instanceof \Exception) {
66 }
67 return $clone->withError("" . $error);
68 }
69 return $clone;
70 }
71 }
72
76 public function getLabel(): string
77 {
78 return $this->label;
79 }
80
84 public function withLabel(string $label): self
85 {
86 $clone = clone $this;
87 $clone->label = $label;
88 return $clone;
89 }
90
94 public function getByline(): ?string
95 {
96 return $this->byline;
97 }
98
102 public function withByline(string $byline): self
103 {
104 $clone = clone $this;
105 $clone->byline = $byline;
106 return $clone;
107 }
108
112 public function isRequired(): bool
113 {
114 return $this->is_required;
115 }
116
120 public function withRequired(bool $is_required, ?Constraint $requirement_constraint = null): self
121 {
122 $clone = clone $this;
123 $clone->is_required = $is_required;
124 $clone->requirement_constraint = ($is_required) ? $requirement_constraint : null;
125 return $clone;
126 }
127
131 public function isDisabled(): bool
132 {
133 return $this->is_disabled;
134 }
135
139 public function withDisabled(bool $is_disabled): self
140 {
141 $clone = clone $this;
142 $clone->is_disabled = $is_disabled;
143 return $clone;
144 }
145
149 public function withOnUpdate(Signal $signal): self
150 {
151 return $this->withTriggeredSignal($signal, 'update');
152 }
153
157 public function appendOnUpdate(Signal $signal): self
158 {
159 return $this->appendTriggeredSignal($signal, 'update');
160 }
161
166 abstract protected function getConstraintForRequirement(): ?Constraint;
167
171 protected function applyOperationsTo($res): Result
172 {
173 if ($res === null && !$this->isRequired()) {
174 return $this->data_factory->ok($res);
175 }
176
178 }
179
183 protected function getOperations(): Generator
184 {
185 if ($this->isRequired()) {
186 $op = $this->getConstraintForRequirement();
187 if ($op !== null) {
188 yield $op;
189 }
190 }
191
192 yield from parent::getOperations();
193 }
194}
Builds data types.
Definition: Factory.php:36
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.static
Definition: FormInput.php:139
withByline(string $byline)
Get an input like this, but with an additional/replaced label.static
Definition: FormInput.php:102
getConstraintForRequirement()
This may return a constraint that will be checked first if the field is required.
withLabel(string $label)
Get an input like this, but with a replaced label.static
Definition: FormInput.php:84
appendOnUpdate(Signal $signal)
Get a component like this, triggering a signal of another component on update.In contrast to withOnUp...
Definition: FormInput.php:157
withOnUpdate(Signal $signal)
Trigger a signal of another component on update.static
Definition: FormInput.php:149
__construct(DataFactory $data_factory, Refinery $refinery, protected string $label, protected ?string $byline=null,)
Definition: FormInput.php:43
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).With the optional $required_constra...
Definition: FormInput.php:120
This implements commonalities between inputs.
Definition: Input.php:43
ilErrorHandling $error
Definition: class.ilias.php:69
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
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
This describes commonalities between all inputs.
Definition: Input.php:47
This interface describes how form inputs are handled internally.
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withInput(InputData $input)
Collects the input, applies trafos and forwards the input to its children and returns a new input gro...
Definition: Group.php:77
appendTriggeredSignal(C\Signal $signal, string $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:47
withTriggeredSignal(C\Signal $signal, string $event)
Add a triggered signal, replacing any other signals registered on the same event.
Definition: Triggerer.php:62
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.