ILIAS  release_7 Revision v7.30-3-g800a261c036
data_processing.php File Reference

Go to the source code of this file.

Functions

 data_processing ()
 Example showing how constraints and transformation can be attached to a form. More...
 

Function Documentation

◆ data_processing()

data_processing ( )

Example showing how constraints and transformation can be attached to a form.

Definition at line 5 of file data_processing.php.

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

References $DIC, and $result.