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