Skip to content

Commit

Permalink
Fix element check in custom-validity plugin (#544)
Browse files Browse the repository at this point in the history
* Fix hello world source

* Fix element check in `custom-validity` plugin

* Add HTMLSelectElement

* Update release note
  • Loading branch information
bencroker authored Jan 28, 2025
1 parent 820f179 commit a1fcf54
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- Changed the `data-on-interval` attribute to accept a `__duration` modifier instead of a `__delay` modifier ([#513](https://github.com/starfederation/datastar/issues/513)).
- Changed the parsing of Datastar expressions so that only semicolons can be used to explicitly indicate a statement delimiter ([#525](https://github.com/starfederation/datastar/issues/525)).
- The `data-custom-validity` attribute can now be used on all valid elements, not just input elements ([534](https://github.com/starfederation/datastar/pull/534)).
- The `data-custom-validity` attribute can now be used on select and textarea elements, in addition to input elements ([534](https://github.com/starfederation/datastar/pull/534)).

### Fixed

Expand Down
10 changes: 5 additions & 5 deletions bundles/datastar.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions bundles/datastar.js.map

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const CustomValidity: AttributePlugin = {
valReq: Requirement.Must,
onLoad: (ctx) => {
const { el, genRX, effect } = ctx
if (!(el instanceof HTMLObjectElement)) {
if (!(el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLTextAreaElement)) {
throw runtimeErr('CustomValidityInvalidElement', ctx)
}
const rx = genRX()
Expand Down
4 changes: 2 additions & 2 deletions sdk/go/consts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion site/routes_errors_runtime.templ
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ templ ComputedValueRequired(name string, info *RuntimeErrorInfo) {

templ CustomValidityInvalidElement(name string, info *RuntimeErrorInfo) {
@RuntimeErrorView(name, info) {
<p>The <code>data-custom-validity</code> attribute was used on an element that is not a <code>HTMLInputElement</code>. The <code>data-custom-validity</code> attribute can only be used on form elements.</p>
<p>The <code>data-custom-validity</code> attribute was used on an element that is not a <code>HTMLInputElement</code>, <code>HTMLSelectElement</code> or <code>HTMLTextAreaElement</code>. The <code>data-custom-validity</code> attribute can only be used on input, select and textarea elements.</p>
@customValidityError()
}
}
Expand Down
15 changes: 7 additions & 8 deletions site/static/md/examples/custom_validity.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

## Demo

<form data-on-submit="@get('/examples/custom_validity/data', {contentType: 'form'})" class="space-y-8">
<form data-signals-bar="" data-on-submit="@get('/examples/custom_validity/data', {contentType: 'form'})" class="space-y-8">
<label class="flex items-center gap-2 input input-bordered">
<input data-bind-foo data-custom-validity="$foo === $bar ? '' : 'Field values must be the same.'" name="foo"
class="grow" placeholder="Type foo contents" />
<input data-bind-foo data-custom-validity="$foo === $bar ? '' : 'Field values must be the same.'" name="foo" class="grow" placeholder="Type foo contents" required />
</label>
<label class="flex items-center gap-2 input input-bordered">
<input data-bind-bar name="bar" class="grow" placeholder="Type bar contents" />
Expand All @@ -19,13 +18,13 @@

## Explanation

The expression passed into the `data-custom-validity` attribute is evaluated and the result is used as the custom
validity message for the input. If the result is an empty string, the input is considered valid. If the result is a
non-empty string, the input is considered invalid and the string is used as the custom validity message.
The expression passed into the `data-custom-validity` attribute is evaluated and the result is used as the custom validity message for the input. If the result is an empty string, the input is considered valid. If the result is a non-empty string, the input is considered invalid and the string is used as the custom validity message.

Note that the signal `bar` must be defined _before_ it is referenced in the `data-custom-validity` attribute expression.

```html
<form data-on-submit="@get('/endpoint', {contentType: 'form'})">
<input data-bind-foo data-custom-validity="$foo === $bar ? '' : 'Field values must be the same.'" name="foo" />
<form data-signals-bar="" data-on-submit="@get('/endpoint', {contentType: 'form'})">
<input data-bind-foo data-custom-validity="$foo === $bar ? '' : 'Field values must be the same.'" name="foo" required />
<input data-bind-bar name="bar" />
<button>
Submit form
Expand Down

0 comments on commit a1fcf54

Please sign in to comment.