ILIAS  release_8 Revision v8.24
data_processing.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
6
11{
12 //Step 0: Declare dependencies
13 global $DIC;
14 $ui = $DIC->ui()->factory();
15 $renderer = $DIC->ui()->renderer();
16 $request = $DIC->http()->request();
17 $refinery = $DIC->refinery();
18 //Step 1: Define transformations
19 $sum = $refinery->custom()->transformation(function ($vs) {
20 list($l, $r) = $vs;
21 $s = $l + $r;
22 return "$l + $r = $s";
23 });
24
25 $from_name = $refinery->custom()->transformation(function ($v) {
26 switch ($v) {
27 case "one": return 1;
28 case "two": return 2;
29 case "three": return 3;
30 case "four": return 4;
31 case "five": return 5;
32 case "six": return 6;
33 case "seven": return 7;
34 case "eight": return 8;
35 case "nine": return 9;
36 case "ten": return 10;
37 }
38 throw new \LogicException("PANIC!");
39 });
40
41 //Step 2: Define custom constraint
42 $valid_number = $refinery->custom()->constraint(function ($v) {
43 return in_array($v, ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]);
44 }, "This is not a number I know...");
45
46 //Step 3: Define the input field and attach the previously defined constraint an
47 // validation.
48 $number_input = $ui->input()->field()
49 ->text("number", "Put in the name of a number from one to ten.")
50 ->withAdditionalTransformation($valid_number)
51 ->withAdditionalTransformation($from_name);
52
53 //Step 4: Define the form action to target the input processing
54 $DIC->ctrl()->setParameterByClass(
55 'ilsystemstyledocumentationgui',
56 'example_name',
57 'data_processing'
58 );
59 $form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
60
61 //Step 5: Define the form, plugin the inputs and attach some transformation acting
62 // on the complete input of the form.
63 $form = $ui->input()->container()->form()->standard(
64 $form_action,
65 [ $number_input->withLabel("Left")
66 , $number_input->withLabel("Right")
67 ]
69
70 //Step 6: Define some data processing.
71 if ($request->getMethod() == "POST"
72 && $request->getQueryParams()['example_name'] == 'data_processing') {
73 $form = $form->withRequest($request);
74 $result = $form->getData();
75 } else {
76 $result = "No result yet.";
77 }
78
79 //Step 7: Render the form and the result of the data processing
80 return
81 "<pre>" . print_r($result, true) . "</pre><br/>" .
82 $renderer->render($form);
83}
global $DIC
Definition: feed.php:28
Refinery Factory $refinery
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
data_processing()
Example showing how constraints and transformation can be attached to a form.