docs(validation): Improved validation guide #484
Mergedjuliendelort(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/484/headmerged by crutchcorn(GitHub profile, opens in a new tab) crutchcorn(GitHub profile, opens in a new tab) requested changes
@@ -209,0 +294,21 @@294+ if (value !== fieldAPI.form.store.state.values.password) {295+ return "Passwords do not match";296+ }297+298+ }}299+ children={(field) => (300+ <>301+ <label htmlFor={field.name}>Repeat password:</label>302+ <input303+ name={field.name}304+ value={field.state.value}305+ type="password"306+ onChange={(e) => field.handleChange(e.target.value)}307+ />308+ <FieldInfo field={field} />309+ </>310+ )}311+/>312+```313+314+@@ -9,8 +11,16 @@9-Asynchronous functional validation10-Adapter-based validation111112-Let's take a look at each and see how they're built.12+## When is validation performed?131314-## Synchronous Functional Validation14+It's up to you! The `<Field />` component accepts some callbacks as props such as `onChange` or `onBlur`. Those callbacks are passed the current value of the field, as well as the fieldAPI object, so that you can perform the validation. If you find a validation error, simply return the error message as string and it will be available in `field.state.meta.errors`.151516-With Form, you can pass a function to a field and, if it returns a string, said string will be used as the error:16+Here is an example:17+18+```tsx19+ <form.Field20+ name="firstName"21+ onChange={(value) => {22+ return !value23+ ? "A first name is required"24+ : value.length < 325+ ? "First name must be at least 3 characters"26+ : undefined;crutchcorn(GitHub profile, opens in a new tab) approved these changes
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
This pull request is automatically built and testable in CodeSandbox.
To see build info of the built libraries, click here or the icon next to each commit SHA.