Forms
The go-advanced-admin panel includes a flexible and powerful forms framework that allows you to handle user input efficiently. This framework is used extensively within the admin panel for creating and editing instances of your models, but it can also be used standalone in other parts of your application.
This guide covers everything you need to know about configuring forms, writing custom fields, integrating forms with your models, and using the forms framework independently.
Overview
The forms framework provides a way to define forms with various field types, handle validation, and render forms in HTML. It is designed to be extensible, allowing you to create custom fields and validation logic as needed.
Key features include:
Multiple Field Types: A variety of built-in field types to capture different kinds of user input.
Validation: Both field-level and form-level validation to ensure data integrity.
Customization: Ability to customize fields and forms, including creating custom field types.
Integration with Models: Seamless integration with your data models for creating and editing records.
Standalone Usage: Use the forms framework independently in other parts of your application.
Form Field Types
The forms framework comes with several built-in field types to handle common data inputs. Each field type has its own properties and validation logic.
TextField
Used for single-line text input.
Properties:
Placeholder
: Placeholder text displayed in the input field.MaxLength
: Maximum number of characters allowed.MinLength
: Minimum number of characters required.Required
: Whether the field is mandatory.Regex
: A regular expression pattern that the input must match.
Example:
IntegerField
Used for integer numbers.
Properties:
MinValue
: Minimum acceptable value.MaxValue
: Maximum acceptable value.Required
: Whether the field is mandatory.
Example:
FloatField
Used for floating-point numbers.
Properties:
MinValue
: Minimum acceptable value.MaxValue
: Maximum acceptable value.Required
: Whether the field is mandatory.
Example:
BooleanField
Used for boolean values (true/false).
Properties:
Required
: Whether the field is mandatory.
Example:
ChoiceField
Used for single selection from a list of choices.
Properties:
Choices
: A list ofChoice
structs, each with aValue
andLabel
.Required
: Whether the field is mandatory.Placeholder
: Placeholder text displayed when no option is selected.
Example:
MultipleChoiceField
Used for multiple selections from a list of choices.
Properties:
Choices
: A list ofChoice
structs.Required
: Whether at least one selection is required.
Example:
DateField
Used for date input.
Properties:
MinDate
: Earliest acceptable date.MaxDate
: Latest acceptable date.Required
: Whether the field is mandatory.Placeholder
: Placeholder text.
Example:
EmailField
Used for email addresses.
Properties:
Required
: Whether the field is mandatory.
Example:
URLField
Used for URLs.
Properties:
Required
: Whether the field is mandatory.
Example:
Validation
Validation ensures that the data submitted through forms meets your application's requirements. The forms framework supports both field-level and form-level validation.
Field-Level Validation
Each field type comes with built-in validation functions based on its properties. For example, setting Required: true
on a TextField
will automatically validate that the field is not empty.
Custom Field Validation
You can add custom validation functions to a field:
Form-Level Validation
Form-level validation functions allow you to validate the form as a whole, possibly checking the relationship between different fields.
Adding Form-Level Validation
Customization
Customizing Fields
You can customize fields by setting their properties or overriding methods.
Setting Superseding Attributes
Superseding attributes allow you to directly set HTML attributes on the field's input element.
Creating Custom Fields
If the built-in field types don't meet your needs, you can create custom fields by implementing the Field
interface.
Field Interface
Example Custom Field
Integrating Forms with Models
The admin panel automatically generates forms for creating and editing instances of your models. You can customize these forms using tags in your model definitions or by implementing interfaces.
Customizing Add and Edit Forms
You can customize the forms used for adding and editing instances by:
Using Struct Tags
Implementing the
AdminFormFieldInterface
Using Tags for Field Configuration
The admin
struct tag allows you to specify how fields are included in forms.
Example Model with Tags
Common Tag Options:
addForm
:include
orexclude
to control inclusion in the add form.editForm
:include
orexclude
to control inclusion in the edit form.required
:true
orfalse
to set the field as required.maxLength
: Maximum length for string fields.minLength
: Minimum length for string fields.placeholder
: Placeholder text for input fields.
Implementing the AdminFormFieldInterface
For more advanced customization, implement the AdminFormFieldInterface
in your model.
Interface Definition
Example Implementation
Using Forms Standalone
The forms framework can be used outside of the admin panel in your application.
Creating a Standalone Form
Define a Form Struct
Rendering the Form
Render the Form in a Template
Example Usage in Handler
Processing Form Data
Handle Form Submission
Next Steps
Explore Advanced Topics: Learn about Customizing the Admin Panel's Look and Security Best Practices.
Check Integrations: See how to integrate with different ORMs and Web Frameworks.
Contribute: If you encounter issues or have suggestions, please
open an issue or submit a pull request.
Feedback
We value your feedback. If you have any suggestions or find something unclear in this guide, please let us know by opening an issue.
Thank you for using go-advanced-admin!