ProcessWire - Formulare mit der API erstellen (Tutorial)

Aus Wikizone
Version vom 17. Dezember 2021, 14:30 Uhr von 134.3.241.3 (Diskussion) (Die Seite wurde neu angelegt: „== Stichworte == TODO - Artikel fertigstellen... CSRF == Allgemeines Vorgehen == ===Drei Schritte=== Formulare benötigen i.d.R. 3 Arbeitsschritte. Die Dars…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Stichworte

TODO - Artikel fertigstellen...


CSRF

Allgemeines Vorgehen

Drei Schritte

Formulare benötigen i.d.R. 3 Arbeitsschritte. Die Darstellung des Formulars, das Validieren der Formulareingaben und die Verarbeitung der Daten.

  • Formularausgabe
  • Validierung der Formulardaten
    • Bei Fehlern in der Eingabe Formular nochmal mit Fehlermeldung und den ausgefüllten Daten anzeigen
  • Formulardaten verarbeiten

Für alle Aufgaben stellt uns ProcessWire Funktionen zur Verfügung Reading now continues through the code comments: <syntaxhighlight lang="php">

// if sumbitted process form if($input->post->submit){

   // process form input
   $form->processInput($input->post);

public function render(): void {

// The function render() creates immediatly a form.
// It will be empty or filled with given values.
// The function createForm() is explained later.

$form = $this->createForm();

// We check if we have a submitted form.
// The Send button is an excellent choice to check,
// but it could have been another field or a Cancel button.
if(input()->post("pf_submit_btn")) {
 // we must first validate
 // The processInput function can be intercepted for later validation.
 // if an error is found, $form->getErrors() will tell us.
  $form->processInput(input()->post);
 // Do we have any errors?
 if(count($form->getErrors()) > 0) {
  // We found errors, we display
  // the form again to correct the situation.
  // The form is filled with error messages above the appropriate fields.
  $this->view->set("form", $form->render());
 } else {
  // No preliminary errors found. We can treat.
  $this->values["company"] = sanitizer()->input()->post("company");
  [...]
  // Are we satisfied with the values?
  // If this is not the case, send the form back to the user!
  // This is the rest of the processing code.
  [...]
 }
} else {
 // We don’t have any data yet, we have to ask for it.
 // The form is empty by default (except for the date in this example).
 // Here we use the Twig rendering engine.
 $this->view->set("form", $form->render());
}

}