ILIAS  trunk Revision v11.0_alpha-1713-gd8962da2f67
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
FormInput.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
32 use Generator;
33 
34 abstract 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) {
65  $error = $error->getMessage();
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 }
This implements commonalities between inputs.
Definition: Input.php:42
$res
Definition: ltiservices.php:66
appendTriggeredSignal(C\Signal $signal, string $event)
Append a triggered signal to other signals of the same event.
Definition: Triggerer.php:47
__construct(DataFactory $data_factory, Refinery $refinery, protected string $label, protected ?string $byline=null,)
Definition: FormInput.php:43
withTriggeredSignal(C\Signal $signal, string $event)
Add a triggered signal, replacing any other signals registered on the same event. ...
Definition: Triggerer.php:62
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
A constraint encodes some resrtictions on values.
Definition: Constraint.php:31
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
getConstraintForRequirement()
This may return a constraint that will be checked first if the field is required. ...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This interface describes how form inputs are handled internally.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
string $error
This is an error on the input as displayed client side.
Definition: Input.php:57
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Definition: FormInput.php:120
__construct(Container $dic, ilPlugin $plugin)
This describes inputs that can be used in forms.
Definition: FormInput.php:32