ILIAS  trunk Revision v11.0_alpha-1749-g1a06bdef097
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
base.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
42 function base()
43 {
44  //Step 0: Declare dependencies
45  global $DIC;
46  $ui = $DIC->ui()->factory();
47  $lng = $DIC->language();
48  $renderer = $DIC->ui()->renderer();
49  $request = $DIC->http()->request();
50  $data = new \ILIAS\Data\Factory();
51  $refinery = $DIC->refinery();
52 
53  //Step 1: Implement transformation and constraints
54  $sum = $refinery->custom()->transformation(function ($vs) {
55  list($l, $r) = $vs;
56  $s = $l + $r;
57  return $s;
58  });
59  $equal_ten = $refinery->custom()->constraint(function ($v) {
60  return $v == 10;
61  }, "The sum must equal ten.");
62 
63  //Step 2: Define inputs
64  $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
65 
66  //Step 3: Define the group, add the inputs to the group and attach the
67  //transformation and constraint
68  $group = $ui->input()->field()->group(
69  [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
70  )
72  ->withAdditionalTransformation($equal_ten);
73 
74  //Step 4: define form and form actions, attach the group to the form
75  $form = $ui->input()->container()->form()->standard('#', ["custom_group" => $group]);
76 
77  //Step 4: Implement some form data processing.
78  if ($request->getMethod() == "POST") {
79  //Step 4.1: Device some context dependant logic to display the potential
80  // constraint error on the group.
81  $form = $form->withRequest($request);
82  $group = $form->getInputs()["custom_group"];
83  if ($group->getError()) {
84  $result = $group->getError();
85  } else {
86  //The result is sumarized through the transformation
87  $result = $form->getData();
88  }
89  } else {
90  $result = "No result yet.";
91  }
92 
93  //Step 5: Return the rendered form
94  return
95  "<pre>" . print_r($result, true) . "</pre><br/>" .
96  $renderer->render($form);
97 }
$renderer
base()
description: > Example showing how groups can be used to attach transformation and constraints on mu...
Definition: base.php:42
global $DIC
Definition: shib_login.php:22
global $lng
Definition: privfeed.php:31
$r