Esempio n. 1
0
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),
		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 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) && !metaData.IsSelfOrParentIndexed(),
		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
}
Esempio n. 3
0
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
}
Esempio n. 4
0
func (self *Form) IsFieldHidden(metaData *model.MetaData) bool {
	if metaData.BoolAttrib("hidden") {
		return true
	}
	selector := metaData.Selector()
	wildcardSelector := metaData.WildcardSelector()
	return utils.StringIn(selector, self.HiddenFields) || utils.StringIn(wildcardSelector, self.HiddenFields)
}
Esempio n. 5
0
func (self *Form) IsFieldDisabled(metaData *model.MetaData) bool {
	if metaData.BoolAttrib("disabled") {
		return true
	}
	selector := metaData.Selector()
	wildcardSelector := metaData.WildcardSelector()
	return utils.StringIn(selector, self.DisabledFields) || utils.StringIn(wildcardSelector, self.DisabledFields)
}
Esempio n. 6
0
// 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 field.BoolAttrib(StructTagKey, "excluded") || 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 {
						config.Logger.Println("Error in view.Form.IsFieldExcluded(): " + err.Error())
					}
				}
				// }
			}
			// No Authenticators passed, thus exclude
			return true
		}
	}
	return false
}
Esempio n. 7
0
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
}
Esempio n. 8
0
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 ModelPhoneController) NewInput(withLabel bool, metaData *model.MetaData, form *Form) (input View, err error) {
	phone := metaData.Value.Addr().Interface().(*model.Phone)
	textField := &TextField{
		Class:       form.FieldInputClass(metaData),
		Name:        metaData.Selector(),
		Text:        phone.Get(),
		Size:        form.GetInputSize(metaData),
		Disabled:    form.IsFieldDisabled(metaData),
		Placeholder: form.InputFieldPlaceholder(metaData),
		Required:    form.IsFieldRequired(metaData) && !metaData.IsSelfOrParentIndexed(),
		Title:       form.FieldLabel(metaData),
	}
	if metaData.BoolAttrib("view", "autofocus") {
		textField.Autofocus = true
	}
	if withLabel {
		return AddStandardLabel(form, textField, metaData), nil
	}
	return textField, nil
}
Esempio n. 10
0
func (self *Ref) Required(metaData *model.MetaData) bool {
	return metaData.BoolAttrib("required")
}
Esempio n. 11
0
func (self *Ref) Required(metaData *model.MetaData) bool {
	return metaData.BoolAttrib(model.StructTagKey, "required")
}
Esempio n. 12
0
// IsFieldHidden returns if a hidden input element will be created for a form field.
// Hidden fields are not validated.
func (self *Form) IsFieldHidden(field *model.MetaData) bool {
	return field.BoolAttrib(StructTagKey, "hidden") || field.SelectorsMatch(self.HiddenFields)
}
Esempio n. 13
0
func (self *Form) IsFieldDisabled(field *model.MetaData) bool {
	return field.BoolAttrib(StructTagKey, "disabled") || field.SelectorsMatch(self.DisabledFields)
}
Esempio n. 14
0
func (self *Form) isFieldRequiredSelectors(metaData *model.MetaData, selector, wildcardSelector string) bool {
	if metaData.BoolAttrib("required") {
		return true
	}
	return utils.StringIn(selector, self.RequiredFields) || utils.StringIn(wildcardSelector, self.RequiredFields)
}