Skip to content

Commit aa39488

Browse files
authored
bool: accept "on" as true because of HTML checkboxes (#17)
1 parent 54b8ddf commit aa39488

4 files changed

Lines changed: 32 additions & 8 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ jobs:
2424
just sysinfo
2525
- name: Run Go Test
2626
run: |
27-
just init tidy lint test
27+
just init tidy lint tests
2828

Justfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@ default:
1313
tidy:
1414
go mod tidy
1515

16+
# run specific unit test
17+
[group('build')]
18+
[no-cd]
19+
test unit:
20+
go test -v -count=1 -race -run {{unit}} 2>/dev/null
21+
1622
# run tests across source tree
1723
[group('build')]
18-
test:
24+
tests:
1925
go test -v -race -count=1 ./...
2026

2127
# apply go vet command on source tree

forms.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,20 +251,23 @@ func BoolOr(b *bool, alt bool) Parser {
251251
}
252252

253253
func (p *boolParser) Parse(values []string) error {
254+
var b bool
255+
var err error
256+
254257
switch {
255258
case len(values) > 1:
256259
return ErrMulitpleValues
257260
case len(values) == 0 && p.required:
258261
return ErrNoValue
259262
case len(values) == 0:
260263
return nil
261-
}
262-
263-
b, err := strconv.ParseBool(values[0])
264-
if err != nil {
265-
return err
264+
case values[0] == "on":
265+
// HTML checkbox uses "on" to indicate checked
266+
b = true
267+
default:
268+
b, err = strconv.ParseBool(values[0])
266269
}
267270

268271
*p.destination = b
269-
return nil
272+
return err
270273
}

forms_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ func Test_Parse_float_malformed(t *testing.T) {
247247
must.Error(t, err)
248248
}
249249

250+
func Test_Parse_Bool_checkbox_on(t *testing.T) {
251+
t.Parallel()
252+
253+
data := url.Values{
254+
"checkbox": []string{"on"},
255+
}
256+
257+
var cb bool
258+
err := ParseValues(data, Schema{
259+
"checkbox": Bool(&cb),
260+
})
261+
must.NoError(t, err)
262+
must.True(t, cb)
263+
}
264+
250265
func Test_Parse_bool_value_missing(t *testing.T) {
251266
t.Parallel()
252267

0 commit comments

Comments
 (0)