Example showing how sections can be used to attach transformation and constraints on multiple fields at once.
Note that sections have a standard way of displaying constraint violations to the user.
8{
9
11 $ui =
$DIC->ui()->factory();
13 $renderer =
$DIC->ui()->renderer();
14 $request =
$DIC->http()->request();
15 $data = new \ILIAS\Data\Factory();
16 $refinery = new \ILIAS\Refinery\Factory(
$data,
$lng);
17
18
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
29 $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
30
31
32
33 $group = $ui->input()->field()->section(
34 [ $number_input->withLabel("Left"), $number_input->withLabel("Right")],
35 "Equals 10",
36 "Left and Right must equal 10"
37 )
38 ->withAdditionalTransformation($sum)
39 ->withAdditionalTransformation($equal_ten);
40
41
42 $form = $ui->input()->container()->form()->standard('#', [$group]);
43
44
45 if ($request->getMethod() == "POST") {
46 $form = $form->withRequest($request);
48 } else {
50 }
51
52
53 return
54 "<pre>" . print_r(
$result,
true) .
"</pre><br/>" .
55 $renderer->render($form);
56}