An open API service indexing awesome lists of open source software.

https://github.com/hidevopsio/formbinder


https://github.com/hidevopsio/formbinder

Last synced: 6 months ago
JSON representation

Awesome Lists containing this project

README

          

Iris Form Binder for `context.ReadForm`
======

form-binder is a clone of the [formam](https://github.com/monoculum/forma) package, slightly edited to use the "form" tag instead of "formam".

Features
--------

* Nesting ad infinitum in `maps`, `structs` and `slices`.
* `UnmarshalText` in values and keys of maps.
* the `map`'s key supported are: `string`, `int` and variants, `uint` and variants, `uintptr`, `float32`, `float64`, `bool`, `struct`, `custom types` to one of the above types registered by function or `UnmarshalText` method, a `pointer` to one of the above types
* A field with `interface{}` that has a `map`, `struct` or `slice` as value is perfectly possible access to them! (see example below)
* decode `time.Time` with format "2006-01-02" by its UnmarshalText method.
* decode `url.URL`
* The `slice` and `array` is possible to access without to indicate a index (If it is the last field, of course)`
* You can to register a `func` for a `custom type` for all fields that include it or one in particular! (see example below)

Types
-----

The supported field types in the destination struct are:

* `string`
* `bool`
* `int`, `int8`, `int16`, `int32`, `int64`
* `uint`, `uint8`, `uint16`, `uint32`, `uint64`
* `float32`, `float64`
* `slice`, `array`
* `struct` and `struct anonymous`
* `map`
* `interface{}`
* `time.Time`
* `url.URL`
* `custom types` to one of the above types
* a `pointer` to one of the above types

**NOTE**: the nesting in `maps`, `structs` and `slices` can be ad infinitum.

Usage
-----

### Client-side - in form html

- Use symbol `.` for access a field of a struct. (i.e, `struct.field1`)
- Use `[]` for access to index of a slice/array. (i.e, `struct.array[0]`). If the array/slice is the last field of the path, it is not necessary to indicate the index
- Use `[]` for access to key of a map. (i.e, `struct.map[es-ES]`).

```html
















```

### Server-Side - in iris app

```go

// file: main.go
package main

import (
"net/url"
"time"

"github.com/kataras/iris"
)

type InterfaceStruct struct {
ID int
Name string
}

type Company struct {
Public bool `form:"public"`
Website url.URL `form:"website"`
Foundation time.Time `form:"foundation"`
Name string
Location struct {
Country string
City string
}
Products []struct {
Name string
Type string
}
Founders []string
Employees int64

Interface interface{}
}

func main() {
app := iris.New()
app.RegisterView(iris.HTML("./views", ".html"))

app.Get("/", func(ctx iris.Context) {
ctx.View("form.html")
})

// IMPORTANT
app.Post("/", func(ctx iris.Context) {
m := Company{
Interface: &InterfaceStruct{}, // its is possible to access to the fields although it's an interface field!
}
err := ctx.ReadForm(&m)
if err != nil {
ctx.StatusCode(iris.StatusBadRequest)
ctx.WriteString(err.Error())
}
})
}
```