ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
base.php File Reference

Go to the source code of this file.

Functions

 base ()
 Example showing how sections can be used to attach transformation and constraints on multiple fields at once. More...
 

Function Documentation

◆ base()

base ( )

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.

Definition at line 7 of file base.php.

8{
9 //Step 0: Declare dependencies
10 global $DIC;
11 $ui = $DIC->ui()->factory();
12 $lng = $DIC->language();
13 $renderer = $DIC->ui()->renderer();
14 $request = $DIC->http()->request();
15 $data = new \ILIAS\Data\Factory();
16 $validation = new \ILIAS\Validation\Factory($data, $lng);
17 $trafo = new \ILIAS\Transformation\Factory();
18
19 //Step 1: Implement transformation and constraints
20 $sum = $trafo->custom(function ($vs) {
21 list($l, $r) = $vs;
22 $s = $l + $r;
23 return $s;
24 });
25 $equal_ten = $validation->custom(function ($v) {
26 return $v == 10;
27 }, "The sum must equal ten");
28
29 //Step 2: Define inputs
30 $number_input = $ui->input()->field()->numeric("number", "Put in a number.");
31
32 //Step 3: Define the group, add the inputs to the group and attach the
33 //transformation and constraint
34 $group = $ui->input()->field()->section(
35 [ $number_input->withLabel("Left"), $number_input->withLabel("Right")],
36 "Equals 10",
37 "Left and Right must equal 10"
38 )
39 ->withAdditionalTransformation($sum)
40 ->withAdditionalConstraint($equal_ten);
41
42 //Step 3, define form and form actions, attach the group to the form
43 $DIC->ctrl()->setParameterByClass(
44 'ilsystemstyledocumentationgui',
45 'example_name',
46 'numeric_inputs'
47 );
48 $form_action = $DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui');
49 $form = $ui->input()->container()->form()->standard($form_action, [$group]);
50
51 //Step 4, implement some form data processing.
52 if ($request->getMethod() == "POST"
53 && $request->getQueryParams()['example_name'] == 'numeric_inputs') {
54 $form = $form->withRequest($request);
55 $result = $form->getData()[0];
56 } else {
57 $result = "No result yet.";
58 }
59
60 //Return the rendered form
61 return
62 "<pre>" . print_r($result, true) . "</pre><br/>" .
63 $renderer->render($form);
64}
$result
global $l
Definition: afr.php:30
foreach($paths as $path) $request
Definition: asyncclient.php:32
$r
Definition: example_031.php:79
$s
Definition: pwgen.php:45
if(isset($_POST['submit'])) $form
global $DIC
Definition: saml.php:7
$lng
$data
Definition: bench.php:6

References $data, $DIC, $form, $l, $lng, $r, $request, $result, and $s.