Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
en:entwickler:index:forms [2025/03/08 09:59] fasseen:entwickler:index:forms [2025/03/09 11:18] (current) fasse
Line 1: Line 1:
 ====== Forms - Create, validate and save ====== ====== Forms - Create, validate and save ======
-==== Introduction ====+===== Introduction =====
 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. 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.
  
-==== Creating an HTML form ====+===== Creating an HTML form =====
 The first step is to create a page with the **PagePresenter** class. The first step is to create a page with the **PagePresenter** class.
 <code php>$page = PagePresenter::withHtmlIDAndHeadline('adm_example_page', $gL10n->get('SYS_EXAMPLES'));</code> <code php>$page = PagePresenter::withHtmlIDAndHeadline('adm_example_page', $gL10n->get('SYS_EXAMPLES'));</code>
Line 74: Line 74:
 Other design elements such as Bootstrap **cards** or notes can be stored directly in the template and do not have to be defined via the **Forms** class as in earlier versions. Other design elements such as Bootstrap **cards** or notes can be stored directly in the template and do not have to be defined via the **Forms** class as in earlier versions.
  
 +===== Validate and save form data =====
 +Once the form has been created, the script for processing the form must now be adapted. The first step is to validate the form content. You can use the following code for this.
 +<code php>$exampleEditForm = $gCurrentSession->getFormObject($_POST['adm_csrf_token']);
 +$formValues = $exampleEditForm->validate($_POST);</code>
 +In the first line, the form is loaded from the session. The form is identified using the CSRF token. Validation then takes place in the next step. This checks whether all mandatory fields have been filled in. It also checks whether the data types correspond to the fields and whether, for example, a valid email address has been entered in an email. In the case of an editor, the content is checked using HTMLPurifier. The exact checks can be looked up in FormPresenter::validate().
 +
 +In the next step, the field contents can now be transferred to the database object and then saved in the database.
 +<code php>
 +$example = new Entity($gDb);
 +$example->readDataByUuid($getAnnUuid);
 +
 +foreach ($formValues as $key => $value) {
 +   if (str_starts_with($key, 'example_')) {
 +      $example->setValue($key, $value);
 +   }
 +}</code>
 +
 +The last step is to return this script. This must be a JSON with the following structure for both a success message and an error.
 +Success:<code json>{
 +   "status": "success"
 +}</code>
 +Error:<code json>{
 +   "status": "error",
 +   "message": "This is the error message that will be shown to the user!"
 +}</code>
 +Now your form is ready and you can try it out in the browser.
  • en/entwickler/index/forms.1741424378.txt.gz
  • Last modified: 2025/03/08 09:59
  • by fasse