This is an old revision of the document!


Forms - Create, validate and save

With version 5, the handling of forms has been improved. Forms can be created in PP with a separate form class from Admidio and a Smarty template. The form sends its entries to the server via an Ajax call and receives a response. This can be an error message if the data was not entered correctly or a success message if the form data could be saved. The data is validated automatically based on the information from the form order.

The first step is to create a page with the PagePresenter class.

$page = PagePresenter::withHtmlIDAndHeadline('adm_example_page', $gL10n->get('SYS_EXAMPLES'));

Now the form can be initialized with the Forms class. A unique ID and the corresponding smarty template must be specified. The URL to which the form input is sent must also be specified.

$form = new FormPresenter(
   'adm_example_edit_form',
   'modules/example.edit.tpl',
   SecurityUtils::encodeUrl(ADMIDIO_URL . FOLDER_MODULES . '/example.php', array(
      'uuid' => $getExampleUUID, 
      'mode' => 'edit'
   )),
   $page
);

Once the form has been initialized, the individual fields can now be added. For each field, there are different transfers, which are documented in the Forms class. The definition of the fields also has an influence on the validation. If a field is defined as a mandatory field. The validation checks that a value is entered for this field. If this is not the case, the validation later returns an error message.

$form->addInput(
   'example_headline',
   $gL10n->get('SYS_TITLE'),
   $example->getValue('example_headline'),
   array('maxLength' => 100, 'property' => FormPresenter::FIELD_REQUIRED)
);
$form->addSelectBox(
   'example_season',
   $gL10n->get('SYS_SEASON'),
   array(
      'winter' => 'SYS_WINTER',
      'spring' => 'SYS_SPRING',
      'summer' => 'SYS_SUMMER',
      'fall' => 'SYS_FALL'
   ),
   array(
      'property' => FormPresenter::FIELD_REQUIRED,
      'defaultValue' => (int)$example->getValue('example_season')
   )
);
$form->addEditor(
   'example_description',
   $gL10n->get('SYS_TEXT'),
   $example->getValue('example_description')
);

In the last step, the form is assigned to the Page Present class and saved in the current session so that the information can be used during validation.

$form->addToHtmlPage();
$gCurrentSession->addFormObject($form);

The corresponding Smarty template looks like this.

  • en/entwickler/index/forms.1741422414.txt.gz
  • Last modified: 2025/03/08 09:26
  • by fasse