ILIAS  release_8 Revision v8.24
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 protected string $label;
43 protected ?string $byline = null;
44
45 public function __construct(
46 DataFactory $data_factory,
47 Refinery $refinery,
48 string $label,
49 ?string $byline = null
50 ) {
51 parent::__construct($data_factory, $refinery);
52 $this->label = $label;
53 $this->byline = $byline;
54 }
55
59 public function withInput(InputData $input)
60 {
61 if (!$this->isDisabled()) {
62 return parent::withInput($input);
63 } else {
64 $clone = $this;
65 $clone->content = $this->applyOperationsTo($clone->getValue());
66 if ($clone->content->isError()) {
67 $error = $clone->content->error();
68 if ($error instanceof \Exception) {
70 }
71 return $clone->withError("" . $error);
72 }
73 return $clone;
74 }
75 }
76
80 public function getLabel(): string
81 {
82 return $this->label;
83 }
84
88 public function withLabel(string $label)
89 {
90 $clone = clone $this;
91 $clone->label = $label;
92 return $clone;
93 }
94
98 public function getByline(): ?string
99 {
100 return $this->byline;
101 }
102
106 public function withByline(string $byline)
107 {
108 $clone = clone $this;
109 $clone->byline = $byline;
110 return $clone;
111 }
112
116 public function isRequired(): bool
117 {
118 return $this->is_required;
119 }
120
124 public function withRequired(bool $is_required, ?Constraint $requirement_constraint = null)
125 {
126 $clone = clone $this;
127 $clone->is_required = $is_required;
128 $clone->requirement_constraint = ($is_required) ? $requirement_constraint : null;
129 return $clone;
130 }
131
135 public function isDisabled(): bool
136 {
137 return $this->is_disabled;
138 }
139
143 public function withDisabled(bool $is_disabled)
144 {
145 $clone = clone $this;
146 $clone->is_disabled = $is_disabled;
147 return $clone;
148 }
149
153 public function withOnUpdate(Signal $signal)
154 {
155 return $this->withTriggeredSignal($signal, 'update');
156 }
157
161 public function appendOnUpdate(Signal $signal): self
162 {
163 return $this->appendTriggeredSignal($signal, 'update');
164 }
165
170 abstract protected function getConstraintForRequirement(): ?Constraint;
171
175 protected function applyOperationsTo($res): Result
176 {
177 if ($res === null && !$this->isRequired()) {
178 return $this->data_factory->ok($res);
179 }
180
182 }
183
187 protected function getOperations(): Generator
188 {
189 if ($this->isRequired()) {
190 $op = $this->getConstraintForRequirement();
191 if ($op !== null) {
192 yield $op;
193 }
194 }
195
196 yield from parent::getOperations();
197 }
198}
Builds data types.
Definition: Factory.php:21
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.static
Definition: FormInput.php:143
withByline(string $byline)
Get an input like this, but with an additional/replaced label.static
Definition: FormInput.php:106
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:88
appendOnUpdate(Signal $signal)
Get a component like this, triggering a signal of another component on update.In contrast to withOnUp...
Definition: FormInput.php:161
withOnUpdate(Signal $signal)
Trigger a signal of another component on update.static
Definition: FormInput.php:153
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:124
__construct(DataFactory $data_factory, Refinery $refinery, string $label, ?string $byline=null)
Definition: FormInput.php:45
This is a legacy support of Implementation\Component\Input\Field\Input that has been moved to Impleme...
Definition: Input.php:32
ilErrorHandling $error
Definition: class.ilias.php:55
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:15
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:32
This describes commonalities between all inputs.
Definition: Input.php:49
This interface describes how form inputs are handled internally.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
$res
Definition: ltiservices.php:69
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Refinery Factory $refinery
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:75
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.