Trongate Website Homepage

Form Handling in Trongate

Trongate provides a structured approach to form handling. Let's outline the process:

The User Experience

  1. The application displays a form to the user.
  2. The user completes the form fields and submits the data.
  3. If there are validation errors, the form is redisplayed with error messages.
  4. This cycle repeats until the form is correctly filled out and validated.

Server-Side Form Handling

On the server side, Trongate applications handle form submissions as follows:

  1. Enforce security checks including automatic Cross-Site Request Forgery (CSRF) protection.
  2. Set up validation rules for form fields using Trongate's Validation Class.
  3. Run the form validation tests.
  4. If validation passes:
    • Collect submitted data.
    • Perform any necessary data transformations (e.g., creating URL-friendly strings).
    • Determine if it's an update to an existing record or a new record insertion.
    • Update or insert the record in the database.
    • Set a flash message to confirm successful operation.
    • Redirect the user to a relevant page (often a 'show' page for the record).
  5. If validation fails:
    • Redisplay the form, typically by calling the method that initially displayed the form.
    • Display form validation errors, using the validation_errors() function.

If all of the above seems overwhelming, you are encouraged to use the Trongate Desktop App for fast and easy automatic code generation.

The remainder of this chapter will serve as a practical tutorial, guiding you through the creation of a form handling system for a 'Students' module.