From f634c3c56be317b9596b62a977b8eda8fe754502 Mon Sep 17 00:00:00 2001 From: George Vilches Date: Wed, 24 Feb 2016 16:55:20 -0500 Subject: [PATCH 1/3] Boolean fields need a SetInitial that correctly parses a boolean value for Checked. --- booleanfield.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/booleanfield.go b/booleanfield.go index 4bceb9a..841fdbd 100644 --- a/booleanfield.go +++ b/booleanfield.go @@ -3,6 +3,7 @@ package gforms import ( "bytes" "reflect" + "strconv" ) // It maps value to FormInstance.CleanedData as type `bool`. @@ -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) From aebfa99464b1d71a7b12d06ed19830781c83e9da Mon Sep 17 00:00:00 2001 From: George Vilches Date: Wed, 24 Feb 2016 17:13:13 -0500 Subject: [PATCH 2/3] Tests of various boolean field rendering behavior. --- booleanfield_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/booleanfield_test.go b/booleanfield_test.go index 8ec4dfe..b3a8bf7 100644 --- a/booleanfield_test.go +++ b/booleanfield_test.go @@ -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 != `` { + 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 != `` { + 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 != `` { + t.Errorf(`Incorrect HTML rendered for SetInitial("true") boolean field: %s`, html) + return + } +} \ No newline at end of file From a99f4d4d408737e85f0964760c19929bf676f5c9 Mon Sep 17 00:00:00 2001 From: George Vilches Date: Wed, 24 Feb 2016 17:19:08 -0500 Subject: [PATCH 3/3] Make the same SetInitial changes to NullBooleanField. --- nullbooleanfield.go | 13 +++++++++++++ nullbooleanfield_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/nullbooleanfield.go b/nullbooleanfield.go index 240462d..59f377b 100644 --- a/nullbooleanfield.go +++ b/nullbooleanfield.go @@ -3,6 +3,7 @@ package gforms import ( "bytes" "reflect" + "strconv" ) // It maps value to FormInstance.CleanedData as type `bool`. @@ -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) diff --git a/nullbooleanfield_test.go b/nullbooleanfield_test.go index 1a90d37..ab51f89 100644 --- a/nullbooleanfield_test.go +++ b/nullbooleanfield_test.go @@ -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 != `` { + 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 != `` { + 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 != `` { + t.Errorf(`Incorrect HTML rendered for SetInitial("true") null boolean field: %s`, html) + return + } +} \ No newline at end of file