ILIAS  release_8 Revision v8.24
base.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
12function 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 = $DIC->refinery();
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()->group(
39 [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
40 )
42 ->withAdditionalTransformation($equal_ten);
43
44 //Step 4: define form and form actions, attach the group to the form
45 $form = $ui->input()->container()->form()->standard('#', ["custom_group" => $group]);
46
47 //Step 4: Implement some form data processing.
48 if ($request->getMethod() == "POST") {
49 //Step 4.1: Device some context dependant logic to display the potential
50 // constraint error on the group.
51 $form = $form->withRequest($request);
52 $group = $form->getInputs()["custom_group"];
53 if ($group->getError()) {
54 $result = $group->getError();
55 } else {
56 //The result is sumarized through the transformation
57 $result = $form->getData();
58 }
59 } else {
60 $result = "No result yet.";
61 }
62
63 //Step 5: Return the rendered form
64 return
65 "<pre>" . print_r($result, true) . "</pre><br/>" .
66 $renderer->render($form);
67}
global $DIC
Definition: feed.php:28
Refinery Factory $refinery
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
base()
Example showing how groups can be used to attach transformation and constraints on multiple fields at...
Definition: base.php:12
$lng