func (self ModelStringController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { str := metaData.Value.Addr().Interface().(*model.String) textField := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: str.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), Required: form.IsFieldRequired(metaData) && !metaData.IsSelfOrParentIndexed(), Title: form.FieldLabel(metaData), } if maxlen, ok, _ := str.Maxlen(metaData); ok { textField.MaxLength = maxlen if maxlen < textField.Size { textField.Size = maxlen } } if metaData.BoolAttrib("view", "search") { textField.Type = SearchTextField } if metaData.BoolAttrib("view", "autofocus") { textField.Autofocus = true } if pattern, ok := metaData.Attrib("view", "pattern"); ok { textField.Pattern = pattern } if withLabel { return AddStandardLabel(form, textField, metaData), nil } return textField, nil }
func (self ModelTextController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { text := metaData.Value.Addr().Interface().(*model.Text) var cols int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "cols"); ok { cols, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } var rows int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "rows"); ok { rows, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } textArea := &TextArea{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: text.Get(), Cols: cols, Rows: rows, Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } if metaData.BoolAttrib("view", "autofocus") { textArea.Autofocus = true } if withLabel { return AddStandardLabel(form, textArea, metaData), nil } return textArea, nil }
// IsFieldExcluded returns weather a field will be excluded from the form. // Fields will be excluded, if their selector matches one in Form.ExcludedFields // or if a matching Authenticator from Form.ModelFieldAuth returns false. // A field is also excluded when its parent field is excluded. // This function is not restricted to model.Value, it works with all struct fields. // This way a whole sub struct an be excluded by adding its selector to Form.ExcludedFields. func (self *Form) IsFieldExcluded(field *model.MetaData, ctx *Context) bool { if field.Parent == nil { return false // can't exclude root } if self.IsFieldExcluded(field.Parent, ctx) || field.SelectorsMatch(self.ExcludedFields) { return true } if len(self.ModelFieldAuth) > 0 { auth, hasAuth := self.ModelFieldAuth[field.Selector()] if !hasAuth { auth, hasAuth = self.ModelFieldAuth[field.WildcardSelector()] } if hasAuth { ok, err := auth.Authenticate(ctx) if err != nil { fmt.Println("Error in view.Form.IsFieldExcluded(): " + err.Error()) } if !ok { return true } } } if len(Config.NamedAuthenticators) > 0 { if authAttrib, ok := field.Attrib(StructTagKey, "auth"); ok { for _, name := range strings.Split(authAttrib, ",") { // if multi := strings.Split(name, "+"); len(multi) > 1 { // // Needs to pass all sub-Authenticators // for _, name := range multi { // if auth, ok := NamedAuthenticator(name); ok { // ok, err := auth.Authenticate(response) // if err != nil { // fmt.Println("Error in view.Form.IsFieldExcluded(): " + err.Error()) // } // if !ok { // return true // } // } // } // } else { if auth, ok := NamedAuthenticator(name); ok { ok, err := auth.Authenticate(ctx) if ok { // Only needs to pass one Authenticator return false } if err != nil { log.Println("Error in view.Form.IsFieldExcluded(): " + err.Error()) } } // } } // No Authenticators passed, thus exclude return true } } return false }
func (self *Form) GetFieldDescription(metaData *model.MetaData) string { selector := metaData.Selector() if desc, ok := self.FieldDescriptions[selector]; ok { return desc } wildcardSelector := metaData.WildcardSelector() if desc, ok := self.FieldDescriptions[wildcardSelector]; ok { return desc } if desc, ok := metaData.Attrib(StructTagKey, "description"); ok { return desc } return "" }
func (self ModelFileController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { accept, _ := metaData.Attrib("view", "accept") form.Enctype = MultipartFormData input = &FileInput{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Accept: accept, Disabled: form.IsFieldDisabled(metaData), } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self *Form) GetInputSize(metaData *model.MetaData) int { selector := metaData.Selector() if size, ok := self.InputSizes[selector]; ok { return size } wildcardSelector := metaData.WildcardSelector() if size, ok := self.InputSizes[wildcardSelector]; ok { return size } if sizeStr, ok := metaData.Attrib("size"); ok { size, _ := strconv.Atoi(sizeStr) return size } layout := self.GetLayout() if layout != nil { return layout.GetDefaultInputSize(metaData) } return 0 }
func (self *MongoRefController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { mongoRef := metaData.Value.Addr().Interface().(*mongo.Ref) valuesAndLabels := []string{"", ""} labelSelectors := self.LabelSelectors if len(labelSelectors) == 0 { if selector, ok := metaData.Attrib("view", "optionsLabel"); ok { labelSelectors = []string{selector} } } i := self.OptionsIterator if i == nil { i = mongoRef.Collection().Iterator() } var doc mongo.DocumentBase for i.Next(&doc) { label, err := mongoRef.Collection().DocumentLabel(doc.ID, labelSelectors...) if err != nil { return nil, err } valuesAndLabels = append(valuesAndLabels, doc.ID.Hex(), label) } if i.Err() != nil { return nil, i.Err() } input = &Select{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Model: &ValueLabelSelectModel{ ValuesAndLabels: valuesAndLabels, SelectedValue: mongoRef.StringID(), }, Size: 1, Disabled: form.IsFieldDisabled(metaData), } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self *Form) GetInputSize(metaData *model.MetaData) int { selector := metaData.Selector() if size, ok := self.InputSizes[selector]; ok { return size } wildcardSelector := metaData.WildcardSelector() if size, ok := self.InputSizes[wildcardSelector]; ok { return size } if sizeStr, ok := metaData.Attrib(StructTagKey, "size"); ok { size, err := strconv.Atoi(sizeStr) if err != nil { panic("Error in Form.GetInputSize(): " + err.Error()) } return size } layout := self.GetLayout() if layout != nil { return layout.GetDefaultInputSize(metaData) } return 0 }
func (self ModelRichTextController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { text := metaData.Value.Addr().Interface().(*model.RichText) var cols int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "cols"); ok { cols, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } var rows int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "rows"); ok { rows, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } var stylesheetURL string if style, ok := metaData.Attrib(StructTagKey, "stylesheetURL"); ok { stylesheetURL = style } input = &RichTextArea{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: text.Get(), Cols: cols, Rows: rows, Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), ToolbarStylesheetURL: stylesheetURL, } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self *StandardFormFieldFactory) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { switch s := metaData.Value.Addr().Interface().(type) { case *model.String: textField := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } if maxlen, ok, _ := s.Maxlen(metaData); ok { textField.MaxLength = maxlen if maxlen < textField.Size { textField.Size = maxlen } } input = textField case *model.Text: var cols int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "cols"); ok { cols, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } var rows int // will be zero if not available, which is OK if str, ok := metaData.Attrib(StructTagKey, "rows"); ok { rows, err = strconv.Atoi(str) if err != nil { panic("Error in StandardFormFieldFactory.NewInput(): " + err.Error()) } } input = &TextArea{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Cols: cols, Rows: rows, Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Url: input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Email: input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Type: EmailTextField, Text: s.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Password: textField := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Type: PasswordTextField, Text: s.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } if maxlen, ok, _ := s.Maxlen(metaData); ok { textField.MaxLength = maxlen if maxlen < textField.Size { textField.Size = maxlen } } input = textField case *model.Phone: input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Bool: checkbox := &Checkbox{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Disabled: form.IsFieldDisabled(metaData), Checked: s.Get(), } if withLabel { checkbox.Label = form.FieldLabel(metaData) } return checkbox, nil case *model.Choice: options := s.Options(metaData) if len(options) == 0 || options[0] != "" { options = append([]string{""}, options...) } input = &Select{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Model: &StringsSelectModel{options, s.Get()}, Disabled: form.IsFieldDisabled(metaData), Size: 1, } case *model.MultipleChoice: options := s.Options(metaData) checkboxes := make(Views, len(options)) for i, option := range options { checkboxes[i] = &Checkbox{ Label: option, Class: form.FieldInputClass(metaData), Name: fmt.Sprintf("%s_%d", metaData.Selector(), i), Disabled: form.IsFieldDisabled(metaData), Checked: s.IsSet(option), } } input = checkboxes case *model.DynamicChoice: options := s.Options() index := s.Index() if len(options) == 0 || options[0] != "" { options = append([]string{""}, options...) index++ } input = &Select{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Model: &IndexedStringsSelectModel{options, index}, Disabled: form.IsFieldDisabled(metaData), Size: 1, } case *model.Country: // todo value := s.String() if value == "" { value = "[empty]" } input = Escape(value) case *model.Date: input = Views{ HTML("(Format: " + model.DateFormat + ")<br/>"), &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Size: len(model.DateFormat), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), }, } case *model.DateTime: input = Views{ HTML("(Format: " + model.DateTimeFormat + ")<br/>"), &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.Get(), Size: len(model.DateTimeFormat), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), }, } case *model.Float: input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.String(), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Int: input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: s.String(), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } case *model.Language: // todo value := s.String() if value == "" { value = "[empty]" } input = HTML(value) case model.Reference: if !form.ShowRefIDs { return nil, nil } value := s.String() if value == "" { value = "[empty]" } input = HTML(value) case *model.GeoLocation: input = HTML(s.String()) case *model.Blob, *model.File: input = &FileInput{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Disabled: form.IsFieldDisabled(metaData), } default: return nil, errs.Format("Unsupported model.Value type '%T' at %s", metaData.Value.Interface(), metaData.Selector()) } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self *Form) FieldInputClass(metaData *model.MetaData) string { class, _ := metaData.Attrib(StructTagKey, "class") return class }
func (self *Form) InputFieldPlaceholder(metaData *model.MetaData) string { if placeholder, ok := metaData.Attrib(StructTagKey, "placeholder"); ok { return placeholder } return "" }
// DirectFieldLabel returns a label for a form field generated from metaData. // It creates the label only from the name or label tag of metaData, // not including its parents. func (self *Form) DirectFieldLabel(metaData *model.MetaData) string { if label, ok := metaData.Attrib(StructTagKey, "label"); ok { return label } return strings.Replace(metaData.NameOrIndex(), "_", " ", -1) }