ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
disabled.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
10 function disabled()
11 {
12  //Step 0: Declare dependencies
13  global $DIC;
14  $ui = $DIC->ui()->factory();
15  $lng = $DIC->language();
16  $renderer = $DIC->ui()->renderer();
17  $request = $DIC->http()->request();
18  $data = new \ILIAS\Data\Factory();
19  $refinery = $DIC->refinery();
20 
21  //Step 1: Implement transformation and constraints
22  $sum = $refinery->custom()->transformation(function ($vs) {
23  list($l, $r) = $vs;
24  $s = $l + $r;
25  return $s;
26  });
27  $equal_ten = $refinery->custom()->constraint(function ($v) {
28  return $v == 10;
29  }, "The sum must equal ten.");
30 
31  //Step 2: Define inputs
32  $number_input = $ui->input()->field()->numeric("number", "Cannot put in a number.")->withValue(5);
33 
34  //Step 3: Define the group, add the inputs to the group and attach the
35  //transformation and constraint
36  $group = $ui->input()->field()->group(
37  [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
38  )->withDisabled(true)
39  ->withAdditionalTransformation($sum)
40  ->withAdditionalTransformation($equal_ten);
41 
42  //Step 4: define form and form actions, attach the group to the form
43  $form = $ui->input()->container()->form()->standard('#', ["custom_group" => $group]);
44 
45  //Step 4: Implement some form data processing.
46  if ($request->getMethod() == "POST") {
47  //Step 4.1: Device some context dependant logic to display the potential
48  // constraint error on the group.
49  $form = $form->withRequest($request);
50  $group = $form->getInputs()["custom_group"];
51  if ($group->getError()) {
52  $result = $group->getError();
53  } else {
54  //The result is summarized through the transformation
55  $result = $form->getData();
56  }
57  } else {
58  $result = "No result yet.";
59  }
60 
61  //Step 5: Return the rendered form
62  return
63  "<pre>" . print_r($result, true) . "</pre><br/>" .
64  $renderer->render($form);
65 }
$lng
disabled()
Example showing how disabled groups can be used.
Definition: disabled.php:10
global $DIC
Definition: feed.php:28
Refinery Factory $refinery