Esempio n. 1
0
// Parse form fields
func Parse(data *config.Config, idx int) (Field, error) {
	base := &BaseField{
		ID:             data.GetRequired("fields[%d].name", idx),
		Name:           data.GetRequired("fields[%d].name", idx),
		Label:          data.GetDefault("fields[%d].label", "", idx),
		Help:           data.GetDefault("fields[%d].help", "", idx),
		Class:          utils.SplitStrList(data.GetDefault("fields[%d].class", "", idx)),
		Size:           utils.SplitStrList(data.GetDefault("fields[%d].size", "", idx)),
		LabelSize:      utils.SplitStrList(data.GetDefault("fields[%d].labelSize", "", idx)),
		Attrs:          parseAttrs(data, "attrs", idx),
		ContainerAttrs: parseAttrs(data, "containerAttrs", idx),
	}

	var field Field
	fieldType := data.GetRequired("fields[%d].type", idx)
	switch fieldType {
	case "email":
		fallthrough
	case "number":
		fallthrough
	case "password":
		fallthrough
	case "file":
		fallthrough
	case "url":
		fallthrough
	case "text":
		field = &inputField{
			BaseField:   base,
			PlaceHolder: data.GetDefault("fields[%d].placeholder", "", idx),
			Prefix:      data.GetDefault("fields[%d].prefix", "", idx),
			Type:        fieldType,
		}

	case "textarea":
		field = &textAreaField{
			BaseField:   base,
			PlaceHolder: data.GetDefault("fields[%d].placeholder", "", idx),
			Rows:        data.GetInt("fields[%d].rows", 3, idx),
		}

	case "submit":
		field = &submitField{
			BaseField: base,
		}

	case "datepicker":
		field = &datepickerField{
			BaseField:   base,
			PlaceHolder: data.GetDefault("fields[%d].placeholder", "", idx),
			DateFormat:  data.GetDefault("fields[%d].dateFormat", "dd/MM/yyyy", idx),
			IsOpen:      data.GetDefault("fields[%d].isOpen", "", idx),
			Options:     data.GetDefault("fields[%d].options", "", idx),
		}

	case "static":
		field = &staticField{
			BaseField: base,
			Content:   data.GetDefault("fields[%d].content", "", idx),
		}

	case "custom":
		field = &customField{
			BaseField: base,
			Content:   data.GetDefault("fields[%d].content", "", idx),
		}
		/*
		   case "select":
		     field = &selectField{
		       BaseField: base,
		       BlankID:     data.GetDefault("fields[%d].blank.id", "", idx),
		       BlankLabel:  data.GetDefault("fields[%d].blank.label", "", idx),
		       Class:       strings.Split(data.GetDefault("fields[%d].class", "", idx), " "),
		       Size:        strings.Split(data.GetDefault("fields[%d].size", "", idx), " "),
		       Help:        data.GetDefault("fields[%d].help", "", idx),
		       ID:          name,
		       Origin:      data.GetRequired("fields[%d].origin", idx),
		       OriginID:    data.GetDefault("fields[%d].originID", "id", idx),
		       OriginLabel: data.GetDefault("fields[%d].originLabel", "label", idx),
		       Watch:       data.GetDefault("fields[%d].watch", "", idx),
		     }*/

	case "checkbox":
		field = &checkboxField{
			BaseField: base,
		}

	default:
		return nil, fmt.Errorf("no field type %s in html mode", fieldType)
	}
	return field, nil
}