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

Go to the source code of this file.

Functions

 show_form_in_modal ()
 

Function Documentation

◆ show_form_in_modal()

show_form_in_modal ( )

Definition at line 2 of file show_form_in_modal.php.

References $_POST, $DIC, $factory, $form, $id, $out, and $valid.

3 {
4  global $DIC;
5  $factory = $DIC->ui()->factory();
6  $renderer = $DIC->ui()->renderer();
7 
8  require_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
9  require_once('./Services/Form/classes/class.ilTextInputGUI.php');
10  require_once('./Services/Form/classes/class.ilCountrySelectInputGUI.php');
11 
12  // Build the form
13  $form = new ilPropertyFormGUI();
14  $form->setId(uniqid('form'));
15  $item = new ilTextInputGUI('Firstname', 'firstname');
16  $item->setRequired(true);
17  $form->addItem($item);
18  $item = new ilTextInputGUI('Lastname', 'lastname');
19  $item->setRequired(true);
20  $form->addItem($item);
21  $form->addItem(new ilCountrySelectInputGUI('Country', 'country'));
22  $form->setFormAction($DIC->ctrl()->getFormActionByClass('ilsystemstyledocumentationgui'));
23  $item = new ilHiddenInputGUI('cmd');
24  $item->setValue('submit');
25  $form->addItem($item);
26 
27  // Build a submit button (action button) for the modal footer
28  $form_id = 'form_' . $form->getId();
29  $submit = $factory->button()->primary('Submit', '#')
30  ->withOnLoadCode(function ($id) use ($form_id) {
31  return "$('#{$id}').click(function() { $('#{$form_id}').submit(); return false; });";
32  });
33 
34  // Check if the form was submitted, if validation fails, show it again in a modal
35  $out = '';
36  $valid = true;
37  if (isset($_POST['cmd']) && $_POST['cmd'] == 'submit') {
38  if ($form->checkInput()) {
39  $panel = $factory->panel()->standard('Form validation successful', $factory->legacy(print_r($_POST, true)));
40  $out = $renderer->render($panel);
41  } else {
42  $form->setValuesByPost();
43  $valid = false;
44  }
45  }
46 
47  $modal = $factory->modal()->roundtrip('User Details', $factory->legacy($form->getHTML()))
48  ->withActionButtons([$submit]);
49 
50  // The modal triggers its show signal on load if validation failed
51  if (!$valid) {
52  $modal = $modal->withOnLoad($modal->getShowSignal());
53  }
54  $button1 = $factory->button()->standard('Show Form', '#')
55  ->withOnClick($modal->getShowSignal());
56 
57  return $renderer->render([$button1, $modal]) . $out;
58 }
This class represents a property form user interface.
global $DIC
Definition: saml.php:7
This class represents a selection list property in a property form.
$valid
$factory
Definition: metadata.php:43
if(!array_key_exists('StateId', $_REQUEST)) $id
This class represents a hidden form property in a property form.
if(isset($_POST['submit'])) $form
This class represents a text property in a property form.
$_POST["username"]