ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
with_required_custom_constraint.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
21 {
22  //Step 0: Declare dependencies
23  global $DIC;
24  $ui = $DIC->ui()->factory();
25  $refinery = $DIC->refinery();
26  $renderer = $DIC->ui()->renderer();
27  $request = $DIC->http()->request();
28 
29  // Step 1: define the text field, make it a required field
30  // and add a custom constraint
31  $text_input = $ui->input()->field()->text("Enter a name", "Needs to start with an H");
32  $custom_constraint = $refinery->custom()->constraint(function ($value) {
33  return (substr($value, 0, 1) === 'H') ? true : false;
34  }, "Name does not start with an H");
35  $text_input = $text_input->withRequired(true, $custom_constraint);
36 
37  //Step 2: define form and form actions
38  $form = $ui->input()->container()->form()->standard('#', [ $text_input]);
39 
40  //Step 3: implement some form data processing.
41  if ($request->getMethod() == "POST") {
42  $form = $form->withRequest($request);
43  $result = $form->getData();
44  } else {
45  $result = "No result yet.";
46  }
47 
48  //Step 4: Render the checkbox with the enclosing form.
49  return
50  "<pre>" . print_r($result, true) . "</pre><br/>" .
51  $renderer->render($form);
52 }
$renderer
with_required_custom_constraint()
description: > Example showing the use of the withRequired() method with a custom constraint that re...
global $DIC
Definition: shib_login.php:25