Example showing how groups can be used to attach transformation and constraints on multiple fields at once.
Note that groups do not have a defined way of outputting validations errors. This is context dependant.
8{
9
11 $ui =
$DIC->ui()->factory();
13 $renderer =
$DIC->ui()->renderer();
15 $data = new \ILIAS\Data\Factory();
16 $validation = new \ILIAS\Validation\Factory(
$data,
$lng);
17 $trafo = new \ILIAS\Transformation\Factory();
18
19
20 $sum = $trafo->custom(function ($vs) {
24 });
25 $equal_ten = $validation->custom(function ($v) {
26 return $v == 10;
27 }, "The sum must equal ten.");
28
29
30 $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
31
32
33
34 $group = $ui->input()->field()->group(
35 [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
36 )
37 ->withAdditionalTransformation($sum)
38 ->withAdditionalConstraint($equal_ten);
39
40
41 $DIC->ctrl()->setParameterByClass(
42 'ilsystemstyledocumentationgui',
43 'example_name',
44 'numeric_inputs'
45 );
46 $form_action =
$DIC->ctrl()->getFormActionByClass(
'ilsystemstyledocumentationgui');
47 $form = $ui->input()->container()->form()->standard($form_action, [
"custom_group" => $group]);
48
49
51 &&
$request->getQueryParams()[
'example_name'] ==
'numeric_inputs') {
52
53
55 $group =
$form->getInputs()[
"custom_group"];
56 if ($group->getError()) {
58 } else {
59
61 }
62 } else {
64 }
65
66
67 return
68 "<pre>" . print_r(
$result,
true) .
"</pre><br/>" .
69 $renderer->render(
$form);
70}
foreach($paths as $path) $request
if(isset($_POST['submit'])) $form