func (self *MongoRefController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { mongoRef := metaData.Value.Addr().Interface().(*mongo.Ref) options := []string{""} for doc := self.OptionsIterator.Next(); doc != nil; doc = self.OptionsIterator.Next() { name := self.getLabel(doc) for _, option := range options { if option == name { return nil, fmt.Errorf("Names of mongo documents must be unique, found double '%s'", name) } } options = append(options, name) } name := "" doc, err := mongoRef.Get() if err != nil { return nil, err } if doc != nil { name = self.getLabel(doc) } input = &Select{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Model: &StringsSelectModel{options, name}, Disabled: form.IsFieldDisabled(metaData), Size: 1, } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
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 ModelPasswordController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { password := metaData.Value.Addr().Interface().(*model.Password) textField := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Type: PasswordTextField, Text: password.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), Required: form.IsFieldRequired(metaData), Title: form.FieldLabel(metaData), } if maxlen, ok, _ := password.Maxlen(metaData); ok { textField.MaxLength = maxlen if maxlen < textField.Size { textField.Size = maxlen } } if metaData.BoolAttrib("view", "autofocus") { textField.Autofocus = true } if withLabel { return AddStandardLabel(form, textField, metaData), nil } return textField, nil }
func (self *setPostValuesStructVisitor) field(field *model.MetaData) error { if self.form.IsFieldDisabled(field) || self.form.IsFieldExcluded(field, self.ctx) { return nil } value := self.ctx.Request.FormValue(field.Selector()) return self.form.GetFieldControllers().SetValue(value, self.ctx, field, self.form) }
func (self SetModelValueControllerBase) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error { mdl, ok := metaData.ModelValue() if !ok { panic("metaData must be model.Value") } mdl.SetString(value) return nil }
// IsFieldExcluded returns weather a field will be excluded. // Fields will be excluded, if their selector matches one in LabeledModelView.ExcludedFields. // 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 LabeledModelView.ExcludedFields. func (self *LabeledModelView) IsFieldExcluded(field *model.MetaData) bool { if field.Parent == nil { return false // can't exclude root } if self.IsFieldExcluded(field.Parent) || field.SelectorsMatch(self.ExcludedFields) { return true } return false }
func (self BlobRefController) NewInput(withLabel bool, metaData *model.MetaData, form *view.Form) (input view.View, err error) { blobRef := metaData.Value.Addr().Interface().(*BlobRef) hiddenInput := &view.HiddenInput{Name: metaData.Selector(), Value: blobRef.String()} uploadList := &view.List{Class: "qq-upload-list"} removeButton := &view.Div{ Class: "qq-upload-button", Content: view.HTML("Remove"), OnClick: fmt.Sprintf( `jQuery("#%s").attr("value", ""); jQuery("#%s").empty();`, hiddenInput.ID(), uploadList.ID(), ), } chooseList := view.Views{ view.JQueryUIAutocompleteFromURL(".gostart-select-blob", API.AllBlobs, 2), } uploadButton := UploadBlobButton( "", "#"+uploadList.ID(), fmt.Sprintf( `function(id, fileName, responseJSON) { jQuery("#%s").attr("value", responseJSON.blobID); }`, hiddenInput.ID(), ), ) editor := view.DIV(Config.BlobRefController.Class, view.RequireScriptURL("/media/media.js", 0), view.RequireStyle( `.qq-upload-button { margin: 0 0 5px 10px; cursor: pointer; } .qq-upload-button:hover { background-color: #cc0000; }`, 10, ), hiddenInput, view.DIV(Config.BlobRefController.ActionsClass, chooseList, removeButton, uploadButton), uploadList, view.DivClearBoth(), ) if withLabel { return view.AddStandardLabel(form, editor, metaData), nil } return editor, nil }
func (self *validateAndFormLayoutStructVisitor) validateGeneral(data *model.MetaData) error { if validator, ok := data.ModelValidator(); ok { err := validator.Validate(data) if err != nil { self.generalValidationErrors = append(self.generalValidationErrors, err) } return err } return nil }
func (self *validateAndFormLayoutStructVisitor) validateGeneral(data *model.MetaData) error { if !self.isPost { return nil } err := data.Validate() if err != nil { self.generalValidationErrors = append(self.generalValidationErrors, err) } return err }
func (self *validateAndFormLayoutStructVisitor) validateField(field *model.MetaData) error { if !self.isPost { return nil } err := field.Validate() if err != nil { self.fieldValidationErrors = append(self.fieldValidationErrors, err) } return err }
func (self ModelBlobController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { input = &FileInput{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Disabled: form.IsFieldDisabled(metaData), } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self ModelMultipleChoiceController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error { m := metaData.Value.Addr().Interface().(*model.MultipleChoice) options := m.Options(metaData) *m = nil for i, option := range options { name := fmt.Sprintf("%s_%d", metaData.Selector(), i) if ctx.Request.FormValue(name) != "" { *m = append(*m, option) } } return nil }
func (self *setPostValuesStructVisitor) trySetFieldValue(field *model.MetaData) error { if self.form.IsFieldDisabled(field) || self.form.IsFieldExcluded(field, self.ctx) { return nil } switch s := field.Value.Addr().Interface().(type) { case model.Reference: // we don't handle references with forms yet case *model.Bool: s.Set(self.ctx.Request.FormValue(field.Selector()) != "") case *model.MultipleChoice: options := s.Options(field) *s = nil for i, option := range options { name := fmt.Sprintf("%s_%d", field.Selector(), i) if self.ctx.Request.FormValue(name) != "" { *s = append(*s, option) } } case *model.Blob: file, _, err := self.ctx.Request.FormFile(field.Selector()) if err != nil { return err } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return err } s.Set(bytes) case *model.File: file, header, err := self.ctx.Request.FormFile(field.Selector()) if err != nil { return err } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return err } s.Name = header.Filename s.Data = bytes case model.Value: // General case s.SetString(self.ctx.Request.FormValue(field.Selector())) } return nil }
func (self *validateAndFormLayoutStructVisitor) validateField(field *model.MetaData) error { if value, ok := field.ModelValue(); ok { err := value.Validate(field) if err == nil && value.IsEmpty() && self.form.IsFieldRequired(field) { err = model.NewRequiredError(field) } if err != nil { self.fieldValidationErrors = append(self.fieldValidationErrors, err) } return err } return nil }
func (self ModelIntController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { i := metaData.Value.Addr().Interface().(*model.Int) input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: i.String(), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
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 ModelBlobController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error { b := metaData.Value.Addr().Interface().(*model.Blob) file, _, err := ctx.Request.FormFile(metaData.Selector()) if err != nil { return err } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return err } b.Set(bytes) return 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 }
func (self ModelFileController) SetValue(value string, ctx *Context, metaData *model.MetaData, form *Form) error { f := metaData.Value.Addr().Interface().(*model.File) file, header, err := ctx.Request.FormFile(metaData.Selector()) if err != nil { return err } defer file.Close() bytes, err := ioutil.ReadAll(file) if err != nil { return err } f.Name = header.Filename f.Data = bytes return nil }
func (self *setPostValuesStructVisitor) BeginSlice(slice *model.MetaData) error { if slice.Value.CanSet() && !self.form.IsFieldExcluded(slice) { if lengthStr := self.context.Request.FormValue(slice.Selector() + ".length"); lengthStr != "" { length, err := strconv.Atoi(lengthStr) if err != nil { panic(err.Error()) } if length != slice.Value.Len() { slice.Value.Set(utils.SetSliceLengh(slice.Value, length)) mongo.InitRefs(self.formModel) } } } return nil }
func (self *setPostValuesStructVisitor) BeginIndexedFields(indexedFields *model.MetaData) error { if indexedFields.Kind == model.SliceKind && indexedFields.Value.CanSet() && !self.form.IsFieldExcluded(indexedFields, self.ctx) { if lengthStr := self.ctx.Request.FormValue(indexedFields.Selector() + ".length"); lengthStr != "" { length, err := strconv.Atoi(lengthStr) if err != nil { panic(err.Error()) } if length != indexedFields.Value.Len() { indexedFields.Value.Set(utils.SetSliceLengh(indexedFields.Value, length)) mongo.InitRefs(self.formModel) } } } return nil }
func (self ModelEmailController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { email := metaData.Value.Addr().Interface().(*model.Email) input = &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Type: EmailTextField, Text: email.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, 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 ModelBoolController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { b := metaData.Value.Addr().Interface().(*model.Bool) checkbox := &Checkbox{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Disabled: form.IsFieldDisabled(metaData), Checked: b.Get(), } if withLabel { labelContent := form.FieldLabel(metaData) if form.IsFieldRequired(metaData) { labelContent = "<span class='required'>*</span> " + labelContent } checkbox.Label = labelContent } return checkbox, nil }
func (self ModelDateTimeController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { dateTime := metaData.Value.Addr().Interface().(*model.DateTime) input = Views{ HTML("(Format: " + model.DateTimeFormat + ")<br/>"), &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: dateTime.Get(), Size: len(model.DateTimeFormat), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), }, } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self ModelMultipleChoiceController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { m := metaData.Value.Addr().Interface().(*model.MultipleChoice) options := m.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: m.IsSet(option), } } if withLabel { return AddStandardLabel(form, checkboxes, metaData), nil } return checkboxes, nil }
func (self ModelChoiceController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { choice := metaData.Value.Addr().Interface().(*model.Choice) options := choice.Options(metaData) if len(options) == 0 || options[0] != "" { options = append([]string{""}, options...) } input = &Select{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Model: &StringsSelectModel{options, choice.Get()}, Disabled: form.IsFieldDisabled(metaData), Size: 1, } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
func (self ModelIntController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) { i := metaData.Value.Addr().Interface().(*model.Int) textField := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: i.String(), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), Required: form.IsFieldRequired(metaData), Title: form.FieldLabel(metaData), } if metaData.BoolAttrib("view", "autofocus") { textField.Autofocus = true } if withLabel { return AddStandardLabel(form, textField, metaData), nil } return textField, nil }
func (self ModelUrlController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (View, error) { url := metaData.Value.Addr().Interface().(*model.Url) input := &TextField{ Class: form.FieldInputClass(metaData), Name: metaData.Selector(), Text: url.Get(), Size: form.GetInputSize(metaData), Disabled: form.IsFieldDisabled(metaData), Placeholder: form.InputFieldPlaceholder(metaData), Required: form.IsFieldRequired(metaData), Title: form.FieldLabel(metaData), } if metaData.BoolAttrib("view", "autofocus") { input.Autofocus = true } if withLabel { return AddStandardLabel(form, input, metaData), nil } return input, nil }
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), } if maxlen, ok, _ := str.Maxlen(metaData); ok { textField.MaxLength = maxlen if maxlen < textField.Size { textField.Size = maxlen } } if withLabel { return AddStandardLabel(form, textField, metaData), nil } return textField, nil }