go-macaron / binding

Package binding is a middleware that provides request data binding and validation for Macaron.
Apache License 2.0
23 stars 17 forks source link

Multiple sub-forms from html not json ? #23

Open rhaamo opened 7 years ago

rhaamo commented 7 years ago

It seems that the JSON part supports having multiple "sub-forms" (slices of structs ?).

I would like to have something like (not File Uploads here, plain HTML input forms):

type ItemFile struct {
    Filename string
    Content string `binding:"Required;Text"`
}
type Item struct {
    Description string `binding:"Required"`
    Files []ItemFile // ?
}

Is it possible to do that ? how should I use the "ItemFile" and naming in html for fields names ?

rhaamo commented 7 years ago

I tried another way with:

type Item struct {
    Description string `binding:"MaxSize(255)"`
    IsPublic    bool   `binding:"Default:1"`
    FilesFilename   []string `binding:"Required;MaxSize(255);MinSizeSlice(1)"`
    FilesContent    []string `binding:"Required;MaxSize(255);MinSizeSlice(1)"`
}

If I set Description to Required, submitting a form with empty fields threw an error (expected). If I leave the struct as-is (no Required for Description, only Files Contents and names), and submit form with files_* empty, it threw no errors. (it should)

I found issue #3 but it does seems to only apply to JSON format parser.

Is there any way to do Validation on slices items builtin in macaron/binding or do I need to make them myself after the non-slice fields validation ?

unknwon commented 7 years ago

Hi, I pushed a patch to fix Required check for slice, please help test!