Form Handling in Trongate
Trongate provides a structured approach to form handling. Let's outline the process:
The User Experience
- The application displays a form to the user.
- The user completes the form fields and submits the data.
- If there are validation errors, the form is redisplayed with error messages.
- 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:
- Enforce security checks including automatic Cross-Site Request Forgery (CSRF) protection.
- Set up validation rules for form fields using Trongate's Validation Class.
- Run the form validation tests.
- 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).
- 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.