Skip to content

Commit

Permalink
Switch back to the interface methods instead trick with BaseField.
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Oct 27, 2012
1 parent 8580c8c commit 35c0c29
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 165 deletions.
50 changes: 24 additions & 26 deletions gaeforms/gaeforms.go → appengine.go
@@ -1,61 +1,60 @@
package gaeforms
// +build appengine

package gforms

import (
"fmt"
"html/template"
"net/url"

"appengine/blobstore"

"github.com/vmihailenco/gforms"
)

//------------------------------------------------------------------------------

func init() {
gforms.Register((*BlobField)(nil), func() interface{} {
Register((*BlobField)(nil), func() interface{} {
return NewBlobField()
})
}

//------------------------------------------------------------------------------

func IsBlobstoreFormValid(f gforms.Form, blobs map[string][]*blobstore.BlobInfo, formValues url.Values) bool {
getValue := func(field gforms.Field) (value interface{}) {
bf := field.ToBaseField()
if bf.IsMultipart {
if bf.IsMulti {
value = blobs[bf.Name]
func IsBlobstoreFormValid(form Form, blobs map[string][]*blobstore.BlobInfo, formValues url.Values) bool {
getValue := func(f Field) (value interface{}) {
if f.IsMultipart() {
if f.IsMulti() {
value = blobs[f.Name()]
} else {
if _, ok := blobs[bf.Name]; ok {
value = blobs[bf.Name][0]
if _, ok := blobs[f.Name()]; ok {
value = blobs[f.Name()][0]
}
}
} else {
if bf.IsMulti {
value = formValues[bf.Name]
if f.IsMulti() {
value = formValues[f.Name()]
} else {
if values, ok := formValues[bf.Name]; ok {
if values, ok := formValues[f.Name()]; ok {
value = values[0]
}
}
}
return
}
return gforms.IsValid(f, getValue)
return IsValid(form, getValue)
}

//------------------------------------------------------------------------------

type BlobField struct {
*gforms.BaseField
*BaseField
}

func (f *BlobField) Value() *blobstore.BlobInfo {
if f.IValue == nil {
if f.iValue == nil {
return nil
}
return f.IValue.(*blobstore.BlobInfo)
return f.iValue.(*blobstore.BlobInfo)
}

func (f *BlobField) Validate(rawValue interface{}) error {
Expand All @@ -68,24 +67,23 @@ func (f *BlobField) Validate(rawValue interface{}) error {
return err
}

f.IValue = value
f.iValue = value
return nil
}

func (f *BlobField) SetInitial(initial *blobstore.BlobInfo) {
f.IValue = initial
f.iValue = initial
}

func (f *BlobField) Render(attrs ...string) template.HTML {
return f.Widget.Render(attrs)
return f.Widget().Render(attrs)
}

func NewBlobField() *BlobField {
return &BlobField{
BaseField: &gforms.BaseField{
Widget: gforms.NewFileWidget(),
IsMultipart: true,
IsRequired: true,
BaseField: &BaseField{
widget: NewFileWidget(),
isMultipart: true,
},
}
}

0 comments on commit 35c0c29

Please sign in to comment.