ILIAS  release_8 Revision v8.24
OptionalGroup.php
Go to the documentation of this file.
1<?php
2
18declare(strict_types=1);
19
21
27use LogicException;
28
32class OptionalGroup extends Group implements I\OptionalGroup
33{
35 use Triggerer;
36
37 protected bool $null_value_was_explicitly_set = false;
38
43 {
44 if ($this->requirement_constraint !== null) {
45 return $this->requirement_constraint;
46 }
47
48 return null;
49 }
50
54 protected function isClientSideValueOk($value): bool
55 {
56 if ($value === null) {
57 return true;
58 }
59 return parent::isClientSideValueOk($value);
60 }
61
62 public function withRequired($is_required, ?Constraint $requirement_constraint = null): I\Input
63 {
65 return FormInput::withRequired($is_required, $requirement_constraint);
66 }
67
68 public function isRequired(): bool
69 {
70 return $this->is_required;
71 }
72
76 public function withValue($value): I\Input
77 {
78 if ($value === null) {
79 $clone = clone $this;
80 $clone->value = $value;
81 $clone->null_value_was_explicitly_set = true;
82 return $clone;
83 }
84
85 $clone = parent::withValue($value);
86 $clone->null_value_was_explicitly_set = false;
87 return $clone;
88 }
89
93 public function getValue()
94 {
95 if ($this->null_value_was_explicitly_set) {
96 return null;
97 }
98 return parent::getValue();
99 }
100
101
105 public function withInput(InputData $input): I\Input
106 {
107 if ($this->getName() === null) {
108 throw new LogicException("Can only collect if input has a name.");
109 }
110
111 if (!$this->isDisabled()) {
112 $value = $input->getOr($this->getName(), null);
113 if ($value === null) {
114 $clone = $this->withValue(null);
115 // Ugly hack to prevent shortcutting behaviour of applyOperationsTo
116 $temp = $clone->is_required;
117 $clone->is_required = true;
118 $clone->content = $clone->applyOperationsTo(null);
119 $clone->is_required = $temp;
120 return $clone;
121 }
122 }
123
124 $clone = parent::withInput($input);
125 // If disabled keep, else false, because the null case is already handled.
126 $clone->null_value_was_explicitly_set = $this->isDisabled() && $this->null_value_was_explicitly_set;
127 return $clone;
128 }
129}
This implements the group input.
Definition: Group.php:43
This is a legacy support of Implementation\Component\Input\Field\Input that has been moved to Impleme...
Definition: Input.php:32
isClientSideValueOk($value)
ATTENTION:GroupInternals::_isClientSideValueOk()
withInput(InputData $input)
Get an input like this with input from post data.static
withValue($value)
Get an input like this with another value displayed on the client side.static
withRequired($is_required, ?Constraint $requirement_constraint=null)
getValue()
Get the value that is displayed in the input client side.mixed
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
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.
getName()
The name of the input as used in HTML.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
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
getValue()
Get the value that is displayed in the input client side.
Definition: Group.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.