Example showing how constraints and transformation can be attached to a form.
6{
7
9 $ui =
$DIC->ui()->factory();
10 $renderer =
$DIC->ui()->renderer();
11 $request =
$DIC->http()->request();
12 $refinery =
$DIC->refinery();
13
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
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
42
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
49 $DIC->ctrl()->setParameterByClass(
50 'ilsystemstyledocumentationgui',
51 'example_name',
52 'data_processing'
53 );
54 $form_action =
$DIC->ctrl()->getFormActionByClass(
'ilsystemstyledocumentationgui');
55
56
57
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
67 if ($request->getMethod() == "POST"
68 && $request->getQueryParams()['example_name'] == 'data_processing') {
69 $form = $form->withRequest($request);
71 } else {
73 }
74
75
76 return
77 "<pre>" . print_r(
$result,
true) .
"</pre><br/>" .
78 $renderer->render($form);
79}