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