form

package module
v0.17.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2025 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Form

Open in Gitpod

License

This project is licensed under the GNU Affero General Public License v3.0 (AGPL-3.0). You can find a copy of the license at https://www.gnu.org/licenses/agpl-3.0.en.html

For commercial use, please use my contact page to obtain a commercial license.

Form Field Types

  • date
  • datetime
  • image
  • htmlarea
  • hidden
  • number
  • password
  • select
  • string
  • table
  • textarea
  • raw

Example Customer Update Form

This is an example taken from real life code of a controller type with a form method. The method returns a customer update Form.

func (controller customerUpdateController) form(data customerUpdateControllerData) *hb.Tag {
	updateCustomerForm := form.NewForm(form.FormOptions{
		ID: "FormCustomerUpdate",
	})
	updateCustomerForm.SetFields([]form.Field{
		{
			Label: "Status",
			Name:  "customer_status",
			Type:  form.FORM_FIELD_TYPE_SELECT,
			Value: data.formStatus,
			Options: []form.FieldOption{
				{
					Value: "Draft",
					Key:   models.CUSTOMER_STATUS_DRAFT,
				},
				{
					Value: "Active",
					Key:   models.CUSTOMER_STATUS_ACTIVE,
				},
				{
					Value: "Inactive",
					Key:   models.CUSTOMER_STATUS_INACTIVE,
				},
				{
					Value: "Deleted",
					Key:   models.CUSTOMER_STATUS_DELETED,
				},
			},
		},
		{
			Label: "Type",
			Name:  "customer_type",
			Type:  form.FORM_FIELD_TYPE_STRING,
			Value: data.formType,
		},
		{
			Label: "First Name",
			Name:  "first_name",
			Type:  form.FORM_FIELD_TYPE_STRING,
			Value: data.formPersonFirstName,
		},
		{
			Label: "Last Name",
			Name:  "last_name",
			Type:  form.FORM_FIELD_TYPE_STRING,
			Value: data.formPersonLastName,
		},
		{
			Label: "Company Name",
			Name:  "company_name",
			Type:  form.FORM_FIELD_TYPE_STRING,
			Value: data.formCompanyName,
		},
		{
			Label: "Country",
			Name:  "country",
			Type:  form.FORM_FIELD_TYPE_SELECT,
			Value: data.formCountry,
			Options: lo.Map(data.countries, func(country models.Country, _ int) form.FieldOption {
				return form.FieldOption{
					Key:   country.IsoCode2(),
					Value: country.Name(),
				}
			}),
		},
		{
			Label:    "Customer ID",
			Name:     "customer_id",
			Type:     form.FORM_FIELD_TYPE_STRING,
			Value:    data.customerID,
			Readonly: true,
		},
	})

	if data.formErrorMessage != "" {
		updateCustomerForm.AddField(form.Field{
			Type:  form.FORM_FIELD_TYPE_RAW,
			Value: swal.Swal(swal.SwalOptions{Icon: "error", Text: data.formErrorMessage}),
		})
	}

	if data.formSuccessMessage != "" {
		updateCustomerForm.AddField(form.Field{
			Type:  form.FORM_FIELD_TYPE_RAW,
			Value: swal.Swal(swal.SwalOptions{Icon: "success", Text: data.formSuccessMessage}),
		})
	}

	return updateCustomerForm.Build()

}

The save button outside of the form method. It uses HTMX for submitting the form.

buttonSave := hb.NewButton().
	Class("btn btn-primary ms-2 float-end").
	Child(hb.NewI().Class("bi bi-save").Style("margin-top:-4px;margin-right:8px;font-size:16px;")).
	HTML("Save").
	HxInclude("#FormCustomerUpdate").
	HxPost(links.NewAdminLinks().CustomerUpdate(data.customerID)).
	HxTarget("#FormCustomerUpdate")

Advanced

Configuring the HTML textarea.

The HTML area uses the Trumbowyg wisiwyg editor. To use it you need to add the following to the web page:

// Trumbowyg requires jQuery >= 1.8
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.3.1.min.js"><\/script>')</script>
<script src="trumbowyg/dist/trumbowyg.min.js"></script>
<link rel="stylesheet" href="trumbowyg/dist/ui/trumbowyg.min.css">

If you need special options for the editor, you can use the Options field as in the example below.

{
	Label: "Content",
	Name:  "post_content",
	Type:  FORM_FIELD_TYPE_TEXTAREA,
	Value: data.formContent,
	Help:  "The content of this blog post to display on the post details page.",
	Options: []form.FieldOption{
		{
			Key: "config",
			Value: `{
btns: [
	['viewHTML'],
	['undo', 'redo'],
	['formatting'],
	['strong', 'em', 'del'],
	['superscript', 'subscript'],
	['link','justifyLeft','justifyRight','justifyCenter','justifyFull'],
	['unorderedList', 'orderedList'],
	['insertImage'],
	['removeformat'],
	['horizontalRule'],
	['fullscreen'],
],
autogrow: true,
removeformatPasted: true,
tagsToRemove: ['script', 'link', 'embed', 'iframe', 'input'],
tagsToKeep: ['hr', 'img', 'i'],
autogrowOnEnter: true,
linkTargets: ['_blank'],
}`,
		},
},

Repeater

The repeater field is a special field that allows you to add and remove a group of fields. Each group of fields is called a "repeater item".

The repeater field is created with the NewRepeater method.

Each field in the repeater item, is given a unique name, prefixed with the repeater name, followed by the field name, and a dynamic index. This allows each field to be identified when posting the form.

fieldRepeater := NewRepeater(RepeaterOptions{
	Name: "REPEATER_NAME",
	Fields: []FieldInterface{
		&Field{
			ID:   "ID_1",
			Name: "FIELD_NAME_1",
			Type: FORM_FIELD_TYPE_STRING,
		},
		&Field{
			ID:   "ID_2",
			Name: "FIELD_NAME_2",
			Type: FORM_FIELD_TYPE_STRING,
		},
	},
	Values: []map[string]string{
		{
			"FIELD_NAME_1": "VALUE_1_01",
			"FIELD_NAME_2": "VALUE_2_01",
		},
		{
			"FIELD_NAME_1": "VALUE_1_02",
			"FIELD_NAME_2": "VALUE_2_02",
		},
	},
	RepeaterAddUrl:      "REPEATER_ADD_URL",
	RepeaterMoveUpUrl:   "REPEATER_MOVE_UP_URL",
	RepeaterMoveDownUrl: "REPEATER_MOVE_DOWN_URL",
	RepeaterRemoveUrl:   "REPEATER_REMOVE_URL",
})

The form when posted will look like this:

```post
// first repeater item
REPEATER_NAME[FIELD_NAME_1][] = VALUE_1_01
REPEATER_NAME[FIELD_NAME_2][] = VALUE_2_01

// second repeater item
REPEATER_NAME[FIELD_NAME_1][] = VALUE_1_02
REPEATER_NAME[FIELD_NAME_2][] = VALUE_2_02

Repeater Similar

https://github.com/alleyinteractive/wordpress-fieldmanager

Documentation

Index

Constants

View Source
const FORM_FIELD_TYPE_BLOCKEDITOR = "blockeditor"
View Source
const FORM_FIELD_TYPE_DATE = "date"
View Source
const FORM_FIELD_TYPE_DATETIME = "datetime"
View Source
const FORM_FIELD_TYPE_HIDDEN = "hidden"
View Source
const FORM_FIELD_TYPE_HTMLAREA = "htmlarea"
View Source
const FORM_FIELD_TYPE_IMAGE = "image"
View Source
const FORM_FIELD_TYPE_NUMBER = "number"
View Source
const FORM_FIELD_TYPE_PASSWORD = "password"
View Source
const FORM_FIELD_TYPE_RAW = "raw"
View Source
const FORM_FIELD_TYPE_SELECT = "select"
View Source
const FORM_FIELD_TYPE_STRING = "string"
View Source
const FORM_FIELD_TYPE_TABLE = "table"
View Source
const FORM_FIELD_TYPE_TEXTAREA = "textarea"

Variables

This section is empty.

Functions

func NewRepeater added in v0.11.0

func NewRepeater(opts RepeaterOptions) *fieldRepeater

Types

type Field

type Field struct {
	ID           string // automatic, if not assigned
	Type         string
	Name         string
	Label        string
	Help         string
	Options      []FieldOption
	OptionsF     func() []FieldOption
	Value        string
	Required     bool
	Readonly     bool
	Disabled     bool
	TableOptions TableOptions
	// BlockEditorOptions BlockEditorOptions
	Placeholder string
	Invisible   bool
	CustomInput hb.TagInterface
}

func NewField added in v0.11.0

func NewField(opts FieldOptions) *Field

func (*Field) BuildFormGroup

func (field *Field) BuildFormGroup(fileManagerURL string) *hb.Tag

func (*Field) GetHelp added in v0.13.0

func (field *Field) GetHelp() string

func (*Field) GetID added in v0.13.0

func (field *Field) GetID() string

func (*Field) GetLabel added in v0.12.0

func (field *Field) GetLabel() string

func (*Field) GetName added in v0.10.0

func (field *Field) GetName() string

func (*Field) GetOptions added in v0.13.0

func (field *Field) GetOptions() []FieldOption

func (*Field) GetOptionsF added in v0.13.0

func (field *Field) GetOptionsF() func() []FieldOption

func (*Field) GetRequired added in v0.12.0

func (field *Field) GetRequired() bool

func (*Field) GetType added in v0.10.0

func (field *Field) GetType() string

func (*Field) GetValue added in v0.10.0

func (field *Field) GetValue() string

func (*Field) IsBlockEditor added in v0.6.0

func (field *Field) IsBlockEditor() bool

func (*Field) IsDate

func (field *Field) IsDate() bool

func (*Field) IsDateTime

func (field *Field) IsDateTime() bool

func (*Field) IsDisabled

func (field *Field) IsDisabled() bool

func (*Field) IsHidden

func (field *Field) IsHidden() bool

func (*Field) IsHtmlArea

func (field *Field) IsHtmlArea() bool

func (*Field) IsImage

func (field *Field) IsImage() bool

func (*Field) IsNumber

func (field *Field) IsNumber() bool

func (*Field) IsPassword

func (field *Field) IsPassword() bool

func (*Field) IsRaw

func (field *Field) IsRaw() bool

func (*Field) IsReadonly

func (field *Field) IsReadonly() bool

func (*Field) IsRequired

func (field *Field) IsRequired() bool

func (*Field) IsSelect

func (field *Field) IsSelect() bool

func (*Field) IsString

func (field *Field) IsString() bool

func (*Field) IsTable added in v0.0.2

func (field *Field) IsTable() bool

func (*Field) IsTextArea

func (field *Field) IsTextArea() bool

func (*Field) SetHelp added in v0.13.0

func (field *Field) SetHelp(fieldHelp string)

func (*Field) SetID added in v0.13.0

func (field *Field) SetID(fieldID string)

func (*Field) SetLabel added in v0.12.0

func (field *Field) SetLabel(fieldLabel string)

func (*Field) SetName added in v0.10.0

func (field *Field) SetName(fieldName string)

func (*Field) SetOptions added in v0.13.0

func (field *Field) SetOptions(fieldOptions []FieldOption)

func (*Field) SetOptionsF added in v0.13.0

func (field *Field) SetOptionsF(fieldOptionsF func() []FieldOption)

func (*Field) SetRequired added in v0.12.0

func (field *Field) SetRequired(fieldRequired bool)

func (*Field) SetType added in v0.10.0

func (field *Field) SetType(fieldType string)

func (*Field) SetValue added in v0.10.0

func (field *Field) SetValue(fieldValue string)

func (*Field) TrumbowygScript added in v0.1.0

func (field *Field) TrumbowygScript() string

type FieldInterface added in v0.10.0

type FieldInterface interface {
	GetID() string
	SetID(fieldID string)
	GetLabel() string
	SetLabel(fieldLabel string)
	GetName() string
	SetName(fieldName string)
	GetHelp() string
	SetHelp(fieldName string)
	GetOptions() []FieldOption
	SetOptions(fieldOptions []FieldOption)
	GetOptionsF() func() []FieldOption
	SetOptionsF(fieldOptionsF func() []FieldOption)
	GetRequired() bool
	SetRequired(fieldRequired bool)
	GetType() string
	SetType(fieldType string)
	GetValue() string
	SetValue(fieldValue string)
	BuildFormGroup(fileManagerURL string) *hb.Tag
	// contains filtered or unexported methods
}

type FieldOption

type FieldOption struct {
	Key   string
	Value string
}

type FieldOptions added in v0.11.0

type FieldOptions struct {
	ID           string // automatic, if not assigned
	Type         string
	Name         string
	Label        string
	Help         string
	Options      []FieldOption
	OptionsF     func() []FieldOption
	Value        string
	Required     bool
	Readonly     bool
	Disabled     bool
	TableOptions TableOptions
	Placeholder  string
	Invisible    bool
	CustomInput  hb.TagInterface
}

type Form

type Form struct {
	// contains filtered or unexported fields
}

func NewForm

func NewForm(opts FormOptions) *Form

func (*Form) AddField

func (form *Form) AddField(field FieldInterface)

func (*Form) Build

func (form *Form) Build() *hb.Tag

func (*Form) GetFields

func (form *Form) GetFields() []FieldInterface

func (*Form) GetFileManagerURL

func (form *Form) GetFileManagerURL() string

func (*Form) SetFields

func (form *Form) SetFields(fields []FieldInterface)

func (*Form) SetFileManagerURL

func (form *Form) SetFileManagerURL(url string)

type FormOptions

type FormOptions struct {
	ActionURL      string           // optional
	ClassName      string           // optional
	ID             string           // optional
	Fields         []FieldInterface // optional
	FileManagerURL string           // optional
	Method         string           // optional

	// HTMX helpers
	HxPost   string // optional
	HxTarget string // optional
	HxSwap   string // optional

}

type RepeaterOptions added in v0.11.0

type RepeaterOptions struct {
	Label               string
	Type                string
	Name                string
	Value               string
	Help                string
	Fields              []FieldInterface
	Values              []map[string]string
	RepeaterAddUrl      string
	RepeaterMoveUpUrl   string
	RepeaterMoveDownUrl string
	RepeaterRemoveUrl   string
}

type TableColumn added in v0.0.2

type TableColumn struct {
	Label string
	Width int
}

type TableOptions added in v0.0.2

type TableOptions struct {
	Header          []TableColumn
	Rows            [][]Field
	RowAddButton    *hb.Tag
	RowDeleteButton *hb.Tag
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址