74 lines
1.2 KiB
Go
74 lines
1.2 KiB
Go
|
package pkg
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"net/mail"
|
||
|
)
|
||
|
|
||
|
type InputField struct {
|
||
|
Name string
|
||
|
Label string
|
||
|
Type string
|
||
|
Value string
|
||
|
Required bool
|
||
|
Errors []error
|
||
|
}
|
||
|
|
||
|
func (field *InputField) Equals(other *InputField) bool {
|
||
|
return field.Value == other.Value
|
||
|
}
|
||
|
|
||
|
func (field *InputField) IsEmpty() bool {
|
||
|
return field.Value == ""
|
||
|
}
|
||
|
|
||
|
func (field *InputField) HasValidEmail() bool {
|
||
|
_, err := mail.ParseAddress(field.Value)
|
||
|
return err == nil
|
||
|
}
|
||
|
|
||
|
type SelectOption struct {
|
||
|
Value string
|
||
|
Label string
|
||
|
}
|
||
|
|
||
|
type SelectField struct {
|
||
|
Name string
|
||
|
Label string
|
||
|
Selected string
|
||
|
Options []*SelectOption
|
||
|
Errors []error
|
||
|
}
|
||
|
|
||
|
func (field *SelectField) HasValidOption() bool {
|
||
|
for _, option := range field.Options {
|
||
|
if option.Value == field.Selected {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func MustGetOptions(ctx context.Context, conn *Conn, sql string, args ...interface{}) []*SelectOption {
|
||
|
rows, err := conn.Query(ctx, sql, args...)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer rows.Close()
|
||
|
|
||
|
var options []*SelectOption
|
||
|
for rows.Next() {
|
||
|
option := &SelectOption{}
|
||
|
err = rows.Scan(&option.Value, &option.Label)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
options = append(options, option)
|
||
|
}
|
||
|
if rows.Err() != nil {
|
||
|
panic(rows.Err())
|
||
|
}
|
||
|
|
||
|
return options
|
||
|
}
|