Welcome to the Finch Advanced Forms Guide! This guide will walk you through the steps to create advanced forms in your Finch application. Whether you're a seasoned developer or just starting, Finch offers a robust set of tools to simplify server-side web app development.
What are Advanced Forms?
Advanced forms in Finch are a way to create complex forms with validation and error handling. They are based on the AdvancedForm class. This class provides a simple way to create forms with validation and error handling. You can use this class to create forms with multiple fields, each with its own validation rules.
It reads the data from the request and validates it against the validation rules. It also provides a way to render the form in the view. You can use this class to create forms for user registration, login, profile update, etc.
Using Advanced Forms
To use advanced forms in your Finch application, you need to create a class that extends the AdvancedForm class. This class should implement the fields method, which returns a list of Field objects. Each Field object represents a field in the form and its validation rules.
Here is an example of how to create an advanced form for user registration:
In the above example, we have created a form with three fields: name, email, and password. Each field has its own validation rules. The csrf() method is used to add a CSRF token to the form. This is a security measure to prevent cross-site request forgery attacks. The csrf() method is provided by the AdvancedForm class.
Rendering the Form
To render the form in the view, you can use the render method provided by the AdvancedForm class. This method returns a string that can be rendered in the view. You can use this string in your view to render the form.
In the above example, we have created an instance of the UserRegistrationForm class. We have then called the render method to get the HTML string of the form. We have added this string to the request parameters with the key form. Finally, we have rendered the view user/registration.j2.html which will render the form.
Validating the Form
To validate the form, you can use the check method provided by the AdvancedForm class. This method validates the form data against the validation rules and returns a boolean value. If the form is valid, it returns true. Otherwise, it returns false.
In the above example, we have created an instance of the UserRegistrationForm class. We have then called the check method to validate the form data. If the form is valid, we can process the form data. Otherwise, we can handle the validation errors.
API Response
This kind as form would return a json response with the form data and validation errors. You can use this data to render the form in the view. You can also use this data to handle the validation errors in the view. Advanced form dettects API requests through /api/ in the url and it will return json response.
Render form in view
to render the form in the view, you can use the following code in your template: