Customizing Forms¶
The Go Advanced Admin Panel provides a flexible form framework that allows you to customize forms and even add your own custom form field types.
Table of Contents¶
- Introduction
- Understanding the Form Framework
- Customizing Existing Forms
- Adding Custom Form Field Types
- Example: Creating a Custom Field
- Registering Custom Fields in Models
- Additional Resources
Introduction¶
Custom forms can enhance data entry and improve user experience by providing more suitable input mechanisms for your data types. This guide will help you customize forms and create custom form field types.
Understanding the Form Framework¶
The form framework is based on the form.Field interface, which defines methods for:
- Rendering HTML: Generating the HTML representation of the form field.
- Type Conversion: Converting between Go types and HTML types.
- Validation: Ensuring that input data meets certain criteria.
Customizing Existing Forms¶
Modifying Field Attributes¶
You can modify attributes such as labels, placeholders, and validation rules directly in your model definitions or by customizing the form fields.
Adding or Removing Fields¶
Control which fields appear in your forms by using struct tags in your models.
- Exclude a Field from a Form:
type User struct {
ID uint `gorm:"primarykey" admin:"addForm:exclude"`
Username string
Email string
}
Custom Validation¶
Add custom validation functions to your form fields to enforce specific rules.
- Register a Validation Function:
field.RegisterValidationFunctions(func(value interface{}) ([]error, error) {
// Your validation logic here
})
Adding Custom Form Field Types¶
If the built-in field types do not meet your requirements, you can create custom field types by implementing the
form.Field interface.
Steps to Add a Custom Field Type¶
- Define a New Field Type: Create a struct that embeds
fields.BaseFieldand implements theform.Fieldinterface.
type RichTextField struct {
fields.BaseField
// Additional properties if needed
}
-
Implement Required Methods:
HTML() (string, error): Return the HTML representation of the field.GoTypeToHTMLType(value interface{}) (form.HTMLType, error): Convert a Go value to an HTML-compatible value.HTMLTypeToGoType(value form.HTMLType) (interface{}, error): Convert an HTML value back to a Go value.GetValidationFunctions() []form.FieldValidationFunc: Return any validation functions.
-
Use the Custom Field: Register the field in your form or model where needed.
Example: Creating a Custom Field¶
Implementing a Rich Text Editor Field¶
- Define the Field:
type RichTextField struct {
fields.BaseField
}
func (f *RichTextField) HTML() (string, error) {
// Return HTML for a rich text editor, e.g., using a JavaScript plugin
html := fmt.Sprintf(
`<textarea name="%s">%s</textarea>`, f.Name, template.HTMLEscapeString(f.InitialValue.(string)),
)
return html, nil
}
func (f *RichTextField) GoTypeToHTMLType(value interface{}) (form.HTMLType, error) {
// Convert Go value to HTML string
strValue, ok := value.(string)
if !ok {
return "", errors.New("value must be a string")
}
return form.HTMLType(strValue), nil
}
func (f *RichTextField) HTMLTypeToGoType(value form.HTMLType) (interface{}, error) {
// Convert HTML string to Go value
return string(value), nil
}
func (f *RichTextField) GetValidationFunctions() []form.FieldValidationFunc {
// Return any validation functions if needed
return nil
}
- Register the Field in a Form:
formInstance.AddField("Content", &RichTextField{})
Registering Custom Fields in Models¶
To use custom fields within your models:
- Implement
AdminFormFieldInterfacein your model:
func (p *Post) AdminFormField(name string, isEdit bool) form.Field {
if name == "Content" {
return &RichTextField{}
}
return nil
}
- Return the Custom Field: Provide your custom field for the specified field name.
Additional Resources¶
Note
This section of the documentation was written with the help of Infuzu AI's tools.