Skip to content

Commit 4f653dd

Browse files
fix: update error message to clarify usage with radio inputs (#16874)
* fix: update error message to clarify usage with radio inputs * show an extra bind checked error when a radio type * typo * replace 'group' binding with , drive-by fix some other messages --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 31541c8 commit 4f653dd

File tree

10 files changed

+40
-10
lines changed

10 files changed

+40
-10
lines changed

.changeset/slow-nails-push.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: update `bind:checked` error message to clarify usage with radio inputs

documentation/docs/03-template-syntax/12-bind.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Since 5.6.0, if an `<input>` has a `defaultValue` and is part of a form, it will
9595
9696
## `<input bind:checked>`
9797

98-
Checkbox and radio inputs can be bound with `bind:checked`:
98+
Checkbox inputs can be bound with `bind:checked`:
9999

100100
```svelte
101101
<label>
@@ -117,6 +117,8 @@ Since 5.6.0, if an `<input>` has a `defaultChecked` attribute and is part of a f
117117
</form>
118118
```
119119

120+
> [!NOTE] Use `bind:group` for radio inputs instead of `bind:checked`.
121+
120122
## `<input bind:indeterminate>`
121123

122124
Checkboxes can be in an [indeterminate](https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/indeterminate) state, independently of whether they are checked or unchecked:

packages/svelte/src/compiler/phases/2-analyze/visitors/BindDirective.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function BindDirective(node, context) {
3333
e.bind_invalid_target(
3434
node,
3535
node.name,
36-
property.valid_elements.map((valid_element) => `<${valid_element}>`).join(', ')
36+
property.valid_elements.map((valid_element) => `\`<${valid_element}>\``).join(', ')
3737
);
3838
}
3939

@@ -67,11 +67,15 @@ export function BindDirective(node, context) {
6767
}
6868
} else {
6969
if (node.name === 'checked' && type?.value[0].data !== 'checkbox') {
70-
e.bind_invalid_target(node, node.name, '<input type="checkbox">');
70+
e.bind_invalid_target(
71+
node,
72+
node.name,
73+
`\`<input type="checkbox">\`${type?.value[0].data === 'radio' ? ` — for \`<input type="radio">\`, use \`bind:group\`` : ''}`
74+
);
7175
}
7276

7377
if (node.name === 'files' && type?.value[0].data !== 'file') {
74-
e.bind_invalid_target(node, node.name, '<input type="file">');
78+
e.bind_invalid_target(node, node.name, '`<input type="file">`');
7579
}
7680
}
7781
}
@@ -94,7 +98,7 @@ export function BindDirective(node, context) {
9498
e.bind_invalid_target(
9599
node,
96100
node.name,
97-
`non-<svg> elements. Use 'clientWidth' for <svg> instead`
101+
`non-\`<svg>\` elements. Use \`bind:clientWidth\` for \`<svg>\` instead`
98102
);
99103
}
100104

packages/svelte/tests/compiler-errors/samples/dynamic-element-binding-invalid/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { test } from '../../test';
33
export default test({
44
error: {
55
code: 'bind_invalid_target',
6-
message: '`bind:value` can only be used with <input>, <textarea>, <select>'
6+
message: '`bind:value` can only be used with `<input>`, `<textarea>`, `<select>`'
77
}
88
});

packages/svelte/tests/validator/samples/binding-dimensions-svg/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "bind_invalid_target",
4-
"message": "`bind:offsetWidth` can only be used with non-<svg> elements. Use 'clientWidth' for <svg> instead",
4+
"message": "`bind:offsetWidth` can only be used with non-`<svg>` elements. Use `bind:clientWidth` for `<svg>` instead",
55
"start": {
66
"line": 5,
77
"column": 5

packages/svelte/tests/validator/samples/binding-input-checked/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "bind_invalid_target",
4-
"message": "`bind:checked` can only be used with <input type=\"checkbox\">",
4+
"message": "`bind:checked` can only be used with `<input type=\"checkbox\">`",
55
"start": {
66
"line": 5,
77
"column": 7

packages/svelte/tests/validator/samples/binding-invalid-on-element-2/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "bind_invalid_target",
4-
"message": "`bind:open` can only be used with <details>",
4+
"message": "`bind:open` can only be used with `<details>`",
55
"start": {
66
"line": 5,
77
"column": 5

packages/svelte/tests/validator/samples/binding-invalid-on-element/errors.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[
22
{
33
"code": "bind_invalid_target",
4-
"message": "`bind:value` can only be used with <input>, <textarea>, <select>",
4+
"message": "`bind:value` can only be used with `<input>`, `<textarea>`, `<select>`",
55
"start": {
66
"line": 5,
77
"column": 5
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "bind_invalid_target",
4+
"message": "`bind:checked` can only be used with `<input type=\"checkbox\">` — for `<input type=\"radio\">`, use `bind:group`",
5+
"start": {
6+
"line": 5,
7+
"column": 20
8+
},
9+
"end": {
10+
"line": 5,
11+
"column": 38
12+
}
13+
}
14+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let foo;
3+
</script>
4+
5+
<input type="radio" bind:checked={foo}>

0 commit comments

Comments
 (0)