ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
base.php
Go to the documentation of this file.
1<?php
7function base()
8{
9 //Step 0: Declare dependencies
10 global $DIC;
11 $ui = $DIC->ui()->factory();
12 $lng = $DIC->language();
13 $renderer = $DIC->ui()->renderer();
14 $request = $DIC->http()->request();
15 $data = new \ILIAS\Data\Factory();
16 $refinery = $DIC->refinery();
17
18 //Step 1: Implement transformation and constraints
19 $sum = $refinery->custom()->transformation(function ($vs) {
20 list($l, $r) = $vs;
21 $s = $l + $r;
22 return $s;
23 });
24 $equal_ten = $refinery->custom()->constraint(function ($v) {
25 return $v == 10;
26 }, "The sum must equal ten.");
27
28 //Step 2: Define inputs
29 $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
30
31 //Step 3: Define the group, add the inputs to the group and attach the
32 //transformation and constraint
33 $group = $ui->input()->field()->group(
34 [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
35 )
36 ->withAdditionalTransformation($sum)
37 ->withAdditionalTransformation($equal_ten);
38
39 //Step 4: define form and form actions, attach the group to the form
40 $form = $ui->input()->container()->form()->standard('#', ["custom_group" => $group]);
41
42 //Step 4: Implement some form data processing.
43 if ($request->getMethod() == "POST") {
44 //Step 4.1: Device some context dependant logic to display the potential
45 // constraint error on the group.
46 $form = $form->withRequest($request);
47 $group = $form->getInputs()["custom_group"];
48 if ($group->getError()) {
49 $result = $group->getError();
50 } else {
51 //The result is sumarized through the transformation
52 $result = $form->getData();
53 }
54 } else {
55 $result = "No result yet.";
56 }
57
58 //Step 5: Return the rendered form
59 return
60 "<pre>" . print_r($result, true) . "</pre><br/>" .
61 $renderer->render($form);
62}
base()
Definition: base.php:4
$result
An exception for terminatinating execution or to throw for unit testing.
$lng
$data
Definition: storeScorm.php:23
$DIC
Definition: xapitoken.php:46