1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
12 // Field represents a data field that may be collected in a web form.
15 Error *ValidationError
16 viewArgs map[string]interface{}
17 controller *Controller
20 func NewField(name string, viewArgs map[string]interface{}) *Field {
21 err, _ := viewArgs["errors"].(map[string]*ValidationError)[name]
22 controller, _ := viewArgs["_controller"].(*Controller)
27 controller: controller,
31 // ID returns an identifier suitable for use as an HTML id.
32 func (f *Field) ID() string {
33 return strings.Replace(f.Name, ".", "_", -1)
36 // Flash returns the flashed value of this Field.
37 func (f *Field) Flash() string {
38 v, _ := f.viewArgs["flash"].(map[string]string)[f.Name]
42 // Options returns the option list of this Field.
43 func (f *Field) Options() []string {
44 if f.viewArgs["options"] == nil {
47 v, _ := f.viewArgs["options"].(map[string][]string)[f.Name]
51 // FlashArray returns the flashed value of this Field as a list split on comma.
52 func (f *Field) FlashArray() []string {
57 return strings.Split(v, ",")
60 // Value returns the current value of this Field.
61 func (f *Field) Value() interface{} {
62 pieces := strings.Split(f.Name, ".")
63 answer, ok := f.viewArgs[pieces[0]]
68 val := reflect.ValueOf(answer)
69 for i := 1; i < len(pieces); i++ {
70 if val.Kind() == reflect.Ptr {
73 val = val.FieldByName(pieces[i])
79 return val.Interface()
82 // ErrorClass returns ErrorCSSClass if this field has a validation error, else empty string.
83 func (f *Field) ErrorClass() string {
85 if errorClass, ok := f.viewArgs["ERROR_CLASS"]; ok {
86 return errorClass.(string)
93 // Get the short name and translate it
94 func (f *Field) ShortName() string {
96 if i := strings.LastIndex(name, "."); i > 0 {
99 return f.Translate(name)
102 // Translate the text
103 func (f *Field) Translate(text string, args ...interface{}) string {
104 if f.controller != nil {
105 text = f.controller.Message(text, args...)