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