ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
with_required_custom_constraint.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
37{
38 //Step 0: Declare dependencies
39 global $DIC;
40 $ui = $DIC->ui()->factory();
41 $refinery = $DIC->refinery();
42 $renderer = $DIC->ui()->renderer();
43 $request = $DIC->http()->request();
44
45 // Step 1: define the text field, make it a required field
46 // and add a custom constraint
47 $text_input = $ui->input()->field()->text("Enter a name", "Needs to start with an H");
48 $custom_constraint = $refinery->custom()->constraint(function ($value) {
49 return (substr($value, 0, 1) === 'H') ? true : false;
50 }, "Name does not start with an H");
51 $text_input = $text_input->withRequired(true, $custom_constraint);
52
53 //Step 2: define form and form actions
54 $form = $ui->input()->container()->form()->standard('#', [ $text_input]);
55
56 //Step 3: implement some form data processing.
57 if ($request->getMethod() == "POST") {
58 $form = $form->withRequest($request);
59 $result = $form->getData();
60 } else {
61 $result = "No result yet.";
62 }
63
64 //Step 4: Render the checkbox with the enclosing form.
65 return
66 "<pre>" . print_r($result, true) . "</pre><br/>" .
67 $renderer->render($form);
68}
$renderer
global $DIC
Definition: shib_login.php:26