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 groups can be used to attach transformation and constraints on multiple fields at once. More...
 

Function Documentation

◆ base()

base ( )

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.

Definition at line 7 of file base.php.

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

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()->group(
35  [ $number_input->withLabel("Left"), $number_input->withLabel("Right")]
36  )
37  ->withAdditionalTransformation($sum)
38  ->withAdditionalConstraint($equal_ten);
39 
40  //Step 4: define form and form actions, attach the group to the form
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  //Step 4: Implement some form data processing.
50  if ($request->getMethod() == "POST"
51  && $request->getQueryParams()['example_name'] == 'numeric_inputs') {
52  //Step 4.1: Device some context dependant logic to display the potential
53  // constraint error on the group.
54  $form = $form->withRequest($request);
55  $group = $form->getInputs()["custom_group"];
56  if ($group->getError()) {
57  $result = $group->getError();
58  } else {
59  //The result is sumarized through the transformation
60  $result = $form->getData();
61  }
62  } else {
63  $result = "No result yet.";
64  }
65 
66  //Step 5: Return the rendered form
67  return
68  "<pre>" . print_r($result, true) . "</pre><br/>" .
69  $renderer->render($form);
70 }
$result
foreach($paths as $path) $request
Definition: asyncclient.php:32
global $DIC
Definition: saml.php:7
$s
Definition: pwgen.php:45
$r
Definition: example_031.php:79
if(isset($_POST['submit'])) $form
$lng
global $l
Definition: afr.php:30
$data
Definition: bench.php:6