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) 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 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