github andyrichardson/fielder v1.0.2

latest releases: v2.2.0, v2.1.1, v2.1.0...
4 years ago

v1.0.2 (2019-11-27)

This is the first public release of Fielder!

About

Fielder has been built from the ground up with a field-first approach to validation.

What does this mean?

  • Validation can easily be added and removed to a form
  • Only validate what the user can see (see cross form validation below)
  • No need for a large set of upfront domain knowledge

Features

⚡️Immediate validation

Synchronous validation will update state immediately in the event of a change/blur.

Fewer renders, better performance and no weird "intermediary states".

🔍 Optimized for flexibility

While Yup is supported, you're not limited to using a large Yup schema. Validation functions receive the form state as well as the field value.

(value, state) =>
  state.otherField.value === "string"
    ? Yup.string()
        .required()
        .validateSync(value)
    : Yup.number().validateSync(value);

🤓User focused API

Users don't want to find out that the value they entered on a previous page is invalid. This is why Fielder encourages field-level validation.

If the field isn't mounted, the value won't be validated. Simple!

💁‍♂️ One way to do things

Fielder has been built with hooks since day one. There aren't any clunky APIs to learn, only useField, useForm and useFormContext.

Your data doesn't need to be coupled to your components (and likely shouldn't be), that's why Fielder doesn't include a component API.

Usage

Setting up a form

useForm is where you initiate your form. In order to expose the form to any child components (and subsequently useField), you'll want to expose it via context.

const myForm = useForm();

return <FielderProvider value={myForm}>{children}</FielderProvider>;

Declaring fields

useField is where you harness the power of Fielder.

const [nameProps, nameMeta] = useField({
  name: 'userName',
  validate: useCallback((v) => Yup.string().required().validateSync(v), []);
});

return (
  <input type="text" {...nameProps} />
  {nameMeta.touched && nameMeta.error && <Error>{nameMeta.error}</Error>}
);

There are a whole number of additional arguments which can be passed to useField which allow you to:

  • Set validation
  • Set when validation is triggered (e.g. on blur, change, etc)
  • Set initial value, error, valid and touched states
  • Set unmount behaviour

Note: Unlike other popular form libraries, Fielder allows you to change config options (such as validation) at any time.

Documentation

For more info and examples, check out the Documentation section.

Don't miss a new fielder release

NewReleases is sending notifications on new releases.