ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
Radio.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
6
13
17class Radio extends Input implements C\Input\Field\Radio
18{
20 use Triggerer;
21
25 protected $options = [];
26
30 protected $bylines = [];
31
35 protected function isClientSideValueOk($value) : bool
36 {
37 return ($value === '' || array_key_exists($value, $this->getOptions()));
38 }
39
43 protected function getConstraintForRequirement()
44 {
45 return null;
46 }
47
51 public function withOption(string $value, string $label, string $byline = null) : C\Input\Field\Radio
52 {
53 $clone = clone $this;
54 $clone->options[$value] = $label;
55 if (!is_null($byline)) {
56 $clone->bylines[$value] = $byline;
57 }
58 return $clone;
59 }
60
64 public function getOptions() : array
65 {
66 return $this->options;
67 }
68
69
70 public function getBylineFor(string $value)
71 {
72 if (!array_key_exists($value, $this->bylines)) {
73 return null;
74 }
75 return $this->bylines[$value];
76 }
77
81 public function withInput(InputData $post_input)
82 {
83 if ($this->getName() === null) {
84 throw new \LogicException("Can only collect if input has a name.");
85 }
86 if (!$this->isDisabled()) {
87 $value = $post_input->getOr($this->getName(), "");
88 $clone = $this->withValue($value);
89 } else {
90 $value = $this->getValue();
91 $clone = $this;
92 }
93
94 $clone->content = $this->applyOperationsTo($value);
95 if ($clone->content->isError()) {
96 return $clone->withError("" . $clone->content->error());
97 }
98
99 $clone->content = $this->applyOperationsTo($value);
100
101 if ($clone->getError()) {
102 $clone->content = $clone->data_factory->error($clone->getError());
103 }
104
105 return $clone;
106 }
107
111 public function getUpdateOnLoadCode() : \Closure
112 {
113 return function ($id) {
114 $code = "$('#$id').on('input', function(event) {
115 il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());
116 });
117 il.UI.input.onFieldUpdate(event, '$id', $('#$id input:checked').val());";
118 return $code;
119 };
120 }
121}
An exception for terminatinating execution or to throw for unit testing.
withOption(string $value, string $label, string $byline=null)
Definition: Radio.php:51
getUpdateOnLoadCode()
Get update code.This method has to return JS code that calls il.UI.filter.onFieldUpdate(event,...
Definition: Radio.php:111
This describes commonalities between all inputs.
Definition: Input.php:32
getValue()
Get the value that is displayed in the input client side.
withValue($value)
Get an input like this with another value displayed on the client side.
isDisabled()
Is this input disabled?
This is what a radio-input looks like.
Definition: Radio.php:11
getOptions()
Get all options as value=>label.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
Describes a source for input names.
Definition: NameSource.php:11
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.