forked from rainycape/gondola
/
choices.go
58 lines (51 loc) · 1.47 KB
/
choices.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package form
import (
"fmt"
"gnd.la/app"
"gnd.la/i18n"
)
const (
notChosen = "___gondola_not_chosen"
// NotChosen is used to indicate that a value from
// a multiple choices input has not been chosen
// by the user. See also Choose.
NotChosen = notChosen
)
var (
// Choose creates a choice entry with the text
// "Please, choose" which generates an error when
// the user does not choose anything.
Choose = &Choice{
Name: i18n.String("form|Please, choose"),
Value: NotChosen,
}
)
// Choice represents a choice in a select or radio field.
type Choice struct {
// Name must be either a string or an i18n.String
// or a fmt.Stringer.
// Other types will panic when rendering the form.
Name interface{}
Value interface{}
}
func (c *Choice) TranslatedName(lang i18n.Languager) string {
switch x := c.Name.(type) {
case string:
return x
case i18n.String:
return x.TranslatedString(lang)
case fmt.Stringer:
return x.String()
}
panic(fmt.Errorf("choice %+v has invalid Name type %T", c, c.Name))
}
// ChoicesProvider is the interface implemented by types
// which contain a field or type select or radio. The
// function will only be called for fields of those types.
// If a type which contains choices does not implement this
// interface, the form will panic on creation.
type ChoicesProvider interface {
// FieldChoices returns the choices for the given field.
// It's called just before the field is rendered.
FieldChoices(ctx *app.Context, field *Field) []*Choice
}