From 5dc4a5e5708fe1180c0d0047a00499d6e6371294 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Fri, 27 Mar 2026 09:45:08 -0500 Subject: [PATCH] bool: accept "on" as true because of HTML checkboxes --- .github/workflows/ci.yaml | 2 +- Justfile | 8 +++++++- forms.go | 15 +++++++++------ forms_test.go | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 171ef13..2e8ec05 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,5 +24,5 @@ jobs: just sysinfo - name: Run Go Test run: | - just init tidy lint test + just init tidy lint tests diff --git a/Justfile b/Justfile index 84f9bfe..aadecb6 100644 --- a/Justfile +++ b/Justfile @@ -13,9 +13,15 @@ default: tidy: go mod tidy +# run specific unit test +[group('build')] +[no-cd] +test unit: + go test -v -count=1 -race -run {{unit}} 2>/dev/null + # run tests across source tree [group('build')] -test: +tests: go test -v -race -count=1 ./... # apply go vet command on source tree diff --git a/forms.go b/forms.go index 177ec0d..cfda088 100644 --- a/forms.go +++ b/forms.go @@ -251,6 +251,9 @@ func BoolOr(b *bool, alt bool) Parser { } func (p *boolParser) Parse(values []string) error { + var b bool + var err error + switch { case len(values) > 1: return ErrMulitpleValues @@ -258,13 +261,13 @@ func (p *boolParser) Parse(values []string) error { return ErrNoValue case len(values) == 0: return nil - } - - b, err := strconv.ParseBool(values[0]) - if err != nil { - return err + case values[0] == "on": + // HTML checkbox uses "on" to indicate checked + b = true + default: + b, err = strconv.ParseBool(values[0]) } *p.destination = b - return nil + return err } diff --git a/forms_test.go b/forms_test.go index 43ac87a..cff1f2d 100644 --- a/forms_test.go +++ b/forms_test.go @@ -247,6 +247,21 @@ func Test_Parse_float_malformed(t *testing.T) { must.Error(t, err) } +func Test_Parse_Bool_checkbox_on(t *testing.T) { + t.Parallel() + + data := url.Values{ + "checkbox": []string{"on"}, + } + + var cb bool + err := ParseValues(data, Schema{ + "checkbox": Bool(&cb), + }) + must.NoError(t, err) + must.True(t, cb) +} + func Test_Parse_bool_value_missing(t *testing.T) { t.Parallel()