ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
OptionalGroup.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Timon Amstutz <timon.amstutz@ilub.unibe.ch> Extended GPL, see
4 docs/LICENSE */
5 
7 
9 use ILIAS\UI\Component as C;
16 
20 class OptionalGroup extends Group implements Field\OptionalGroup
21 {
23  use Triggerer;
24 
28  protected $null_value_was_explicitly_set = false;
29 
33  protected function getConstraintForRequirement()
34  {
35  return null;
36  }
37 
41  protected function isClientSideValueOk($value) : bool
42  {
43  if ($value === null) {
44  return true;
45  }
46  return parent::isClientSideValueOk($value);
47  }
48 
49  public function withRequired($is_required)
50  {
51  return Input::withRequired($is_required);
52  }
53 
54  public function isRequired(): bool
55  {
56  return $this->is_required;
57  }
58 
63  public function withValue($value)
64  {
65  if ($value === null) {
66  $clone = clone $this;
67  $clone->value = $value;
68  $clone->null_value_was_explicitly_set = true;
69  return $clone;
70  }
74  $clone = parent::withValue($value);
75  $clone->null_value_was_explicitly_set = false;
76  return $clone;
77  }
78 
82  public function getValue()
83  {
84  if ($this->null_value_was_explicitly_set) {
85  return null;
86  }
87  return parent::getValue();
88  }
89 
90 
94  public function withInput(InputData $post_input)
95  {
96  if ($this->getName() === null) {
97  throw new \LogicException("Can only collect if input has a name.");
98  }
99 
100  if (!$this->isDisabled()) {
101  $value = $post_input->getOr($this->getName(), null);
102  if ($value === null) {
103  $clone = $this->withValue(null);
104  // Ugly hack to prevent shortcutting behaviour of applyOperationsTo
105  $temp = $clone->is_required;
106  $clone->is_required = true;
107  $clone->content = $clone->applyOperationsTo(null);
108  $clone->is_required = $temp;
109  return $clone;
110  }
111  }
112  return parent::withInput($post_input);
113  }
114 }
This describes a group of inputs.
Definition: Group.php:13
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
This describes optional group inputs.
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:12
withRequired($is_required)
Get an input like this, but set the field to be required (or not).
getValue()
Get the value that is displayed in the input client side.mixed
withValue($value)
Get an input like this with another value displayed on the client side.
isDisabled()
Is this input disabled?