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 
26 function base()
27 {
28  //Step 0: Declare dependencies
29  global $DIC;
30  $ui = $DIC->ui()->factory();
31  $lng = $DIC->language();
32  $renderer = $DIC->ui()->renderer();
33  $request = $DIC->http()->request();
34  $data = new \ILIAS\Data\Factory();
35  $refinery = $DIC->refinery();
36 
37  //Step 1: Implement transformation and constraints
38  $sum = $refinery->custom()->transformation(function ($vs) {
39  list($l, $r) = $vs;
40  $s = $l + $r;
41  return $s;
42  });
43  $equal_ten = $refinery->custom()->constraint(function ($v) {
44  return $v == 10;
45  }, "The sum must equal ten.");
46 
47  //Step 2: Define inputs
48  $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
49 
50  //Step 3: Define the group, add the inputs to the group and attach the
51  //transformation and constraint
52  $group = $ui->input()->field()->group(
53  [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
54  )
56  ->withAdditionalTransformation($equal_ten);
57 
58  //Step 4: define form and form actions, attach the group to the form
59  $form = $ui->input()->container()->form()->standard('#', ["custom_group" => $group]);
60 
61  //Step 4: Implement some form data processing.
62  if ($request->getMethod() == "POST") {
63  //Step 4.1: Device some context dependant logic to display the potential
64  // constraint error on the group.
65  $form = $form->withRequest($request);
66  $group = $form->getInputs()["custom_group"];
67  if ($group->getError()) {
68  $result = $group->getError();
69  } else {
70  //The result is sumarized through the transformation
71  $result = $form->getData();
72  }
73  } else {
74  $result = "No result yet.";
75  }
76 
77  //Step 5: Return the rendered form
78  return
79  "<pre>" . print_r($result, true) . "</pre><br/>" .
80  $renderer->render($form);
81 }
$renderer
base()
description: > Example showing how groups can be used to attach transformation and constraints on mu...
Definition: base.php:26
global $DIC
Definition: shib_login.php:25
global $lng
Definition: privfeed.php:32
$r