You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Enable the FormValidityObserver to Be Used in "Manual Only" Mode
This "Manual Mode" can be enabled by passing `null` to the `type`
argument of the constructor. The main reason that this is useful
is that it gives developers a way to only validate their forms
`onsubmit` if they so please.
Copy file name to clipboardexpand all lines: docs/form-validity-observer/README.md
+8-3
Original file line number
Diff line number
Diff line change
@@ -87,9 +87,14 @@ As expected for any form validation library, we also support the following featu
87
87
The `FormValidityObserver()` constructor creates a new observer and configures it with the `options` that you pass in. Because the `FormValidityObserver` only focuses on one task, it has a simple constructor with no overloads.
A string representing the type of event that should cause a form's field to be validated. As with the <code>FormObserver</code>, the string can be a <a href="https://developer.mozilla.org/en-US/docs/Web/Events">commonly recognized</a> event type <em>or</em> your own <a href="../form-observer/guides.md#supporting-custom-event-types">custom</a> event type. But in the case of the <code>FormValidityObserver</code>, only one event type may be specified.
92
+
<p>
93
+
A string representing the type of event that should cause a form's field to be validated. As with the <code>FormObserver</code>, the string can be a <a href="https://developer.mozilla.org/en-US/docs/Web/Events">commonly recognized</a> event type <em>or</em> your own <a href="../form-observer/guides.md#supporting-custom-event-types">custom</a> event type. But in the case of the <code>FormValidityObserver</code>, only one event type may be specified.
94
+
</p>
95
+
<p>
96
+
If you <em>only</em> want to validate fields manually, you can specify <code>null</code> instead of an event type. This can be useful, for instance, if you only want to validate your fields <code>onsubmit</code>. (You would still need to call <a href="#method-formvalidityobservervalidatefieldsoptions-validatefieldsoptions-boolean--promiseboolean"><code>FormValidityObserver.validateFields()</code></a> manually in your <code>submit</code> handler in that scenario.)
Instructs the observer to validate any fields (belonging to the provided form) that a user interacts with, and registers the observer's validation methods with the provided form. Automatic field validation will only occur when a field belonging to the form emits an event matching one of the `types` that were specified during the observer's construction. Unlike the `FormObserver` and the `FormStorageObserver`, _the `FormValidityObserver` may only observe 1 form at a time_.
177
+
Instructs the observer to validate any fields (belonging to the provided form) that a user interacts with, and registers the observer's validation methods with the provided form. Automatic field validation will only occur when a field belonging to the form emits an event matching the `type` that was specified during the observer's construction. Unlike the `FormObserver` and the `FormStorageObserver`, _the `FormValidityObserver` may only observe 1 form at a time_.
173
178
174
179
Note that the `name` attribute is what the observer uses to [identify fields](https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormControlsCollection/namedItem) during manual form validation and error handling. Therefore, a valid `name` is required for all validated fields. **If a field does not have a `name`, then it _will not_ participate in form validation.** Since the [web specification](https://www.w3.org/TR/html401/interact/forms.html#successful-controls) does not allow nameless fields to participate in form submission, this is likely a requirement that your application already satisfies.
Copy file name to clipboardexpand all lines: docs/form-validity-observer/integrations/README.md
+14-15
Original file line number
Diff line number
Diff line change
@@ -128,16 +128,16 @@ We'll walk you through the process by going step-by-step on how we made our `Sve
128
128
The first step is easy. Just create a function that instantiates and returns a `FormValidityObserver`. Because this function will only be creating an augmented `FormValidityObserver`, it should accept the same arguments as the class's [constructor](../README.md#constructor-formvalidityobservertypes-options). The return type will be an `interface` that represents the enhanced observer, but we won't add anything to it yet.
129
129
130
130
```ts
131
-
import type { EventType, OneOrMany, ValidatableField, FormValidityObserverOptions } from "@form-observer/core";
131
+
import type { EventType, ValidatableField, FormValidityObserverOptions } from "@form-observer/core";
>(types:T, options?:FormValidityObserverOptions<M, E, R>): SvelteFormValidityObserver<M, R> {
140
-
constobserver=newFormValidityObserver(types, options) as unknown as SvelteFormValidityObserver<M, R>;
139
+
>(type:T, options?:FormValidityObserverOptions<M, E, R>): SvelteFormValidityObserver<M, R> {
140
+
constobserver=newFormValidityObserver(type, options) as unknown as SvelteFormValidityObserver<M, R>;
141
141
return observer;
142
142
}
143
143
@@ -161,12 +161,12 @@ In order to ensure that all of the `FormValidityObserver`'s methods function pro
161
161
// Imports ...
162
162
163
163
function createFormValidityObserver<
164
-
T extends OneOrMany<EventType>,
164
+
T extends EventType | null,
165
165
M = string,
166
166
E extends ValidatableField = ValidatableField,
167
167
R extends boolean = false,
168
-
>(types:T, options?:FormValidityObserverOptions<M, E, R>): SvelteFormValidityObserver<M, R> {
169
-
constobserver=newFormValidityObserver(types, options) as unknown as SvelteFormValidityObserver<M, R>;
168
+
>(type:T, options?:FormValidityObserverOptions<M, E, R>): SvelteFormValidityObserver<M, R> {
169
+
constobserver=newFormValidityObserver(type, options) as unknown as SvelteFormValidityObserver<M, R>;
170
170
171
171
/* ---------- Bindings ---------- */
172
172
// Form Observer Methods
@@ -199,17 +199,17 @@ In this step, we create a reusable utility function that will enable us to autom
199
199
Most JS frameworks create a way for you to accomplish this easily with utility functions. In [`React`](https://react.dev/reference/react-dom/components/common#ref-callback) or [`Vue`](https://vuejs.org/api/built-in-special-attributes.html#ref), you would pass a `ref` callback to an `HTMLFormElement`. In `Svelte`, the idiomatic way to accomplish this is with [`actions`](https://learn.svelte.dev/tutorial/actions):
0 commit comments