Skip to content
This repository has been archived by the owner on Feb 3, 2019. It is now read-only.

Boolean fields need a SetInitial that correctly parses a boolean value for Checked. #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions booleanfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gforms
import (
"bytes"
"reflect"
"strconv"
)

// It maps value to FormInstance.CleanedData as type `bool`.
Expand Down Expand Up @@ -65,6 +66,18 @@ func (f *BooleanFieldInstance) Clean(data Data) error {
return nil
}

func (f *BooleanFieldInstance) SetInitial(v string) {
f.V.RawStr = v
f.V.RawValue = []string{v}
f.V.IsNil = false
b, err := strconv.ParseBool(v)
if err != nil {
f.V.IsNil = true
} else {
f.V.Value = b
}
}

func (f *BooleanFieldInstance) html() string {
var buffer bytes.Buffer
cx := new(booleanContext)
Expand Down
40 changes: 40 additions & 0 deletions booleanfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,43 @@ func TestFalseBooleanField(t *testing.T) {
t.Error("validation error.")
}
}

func TestBooleanFieldDefaultRender(t *testing.T) {
Form := DefineForm(NewFields(
NewBooleanField("check", nil),
))
form := Form()
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check">` {
t.Errorf(`Incorrect HTML rendered for default boolean field: %s`, html)
return
}
}

func TestBooleanFieldInitialFalseRender(t *testing.T) {
Form := DefineForm(NewFields(
NewBooleanField("check", nil),
))
form := Form()
field, _ := form.GetField("check")
field.SetInitial("false")
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check">` {
t.Errorf(`Incorrect HTML rendered for SetInitial("false") boolean field: %s`, html)
return
}
}

func TestBooleanFieldInitialTrueRender(t *testing.T) {
Form := DefineForm(NewFields(
NewBooleanField("check", nil),
))
form := Form()
field, _ := form.GetField("check")
field.SetInitial("true")
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check" checked>` {
t.Errorf(`Incorrect HTML rendered for SetInitial("true") boolean field: %s`, html)
return
}
}
13 changes: 13 additions & 0 deletions nullbooleanfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gforms
import (
"bytes"
"reflect"
"strconv"
)

// It maps value to FormInstance.CleanedData as type `bool`.
Expand Down Expand Up @@ -63,6 +64,18 @@ func (f *NullBooleanFieldInstance) Clean(data Data) error {
return nil
}

func (f *NullBooleanFieldInstance) SetInitial(v string) {
f.V.RawStr = v
f.V.RawValue = []string{v}
f.V.IsNil = false
b, err := strconv.ParseBool(v)
if err != nil {
f.V.IsNil = true
} else {
f.V.Value = b
}
}

func (f *NullBooleanFieldInstance) html() string {
var buffer bytes.Buffer
cx := new(nullBooleanContext)
Expand Down
40 changes: 40 additions & 0 deletions nullbooleanfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,43 @@ func TestTrueNullBooleanFieldJsonRequired(t *testing.T) {
t.Error("Null boolean field should be required.")
}
}

func TestNullBooleanFieldDefaultRender(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
form := Form()
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check">` {
t.Errorf(`Incorrect HTML rendered for default null boolean field: %s`, html)
return
}
}

func TestNullBooleanFieldInitialFalseRender(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
form := Form()
field, _ := form.GetField("check")
field.SetInitial("false")
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check">` {
t.Errorf(`Incorrect HTML rendered for SetInitial("false") null boolean field: %s`, html)
return
}
}

func TestNullBooleanFieldInitialTrueRender(t *testing.T) {
Form := DefineForm(NewFields(
NewNullBooleanField("check", nil),
))
form := Form()
field, _ := form.GetField("check")
field.SetInitial("true")
html := strings.TrimSpace(form.Html())
if html != `<input type="checkbox" name="check" checked>` {
t.Errorf(`Incorrect HTML rendered for SetInitial("true") null boolean field: %s`, html)
return
}
}