Skip to content

Releases: akuzko/re-use-form

v3.10.1

19 Apr 09:28
Compare
Choose a tag to compare

Patch Description

  • Fixed 'always' on-change strategy to actually be applied on any input update instead of any action being dispatched. This prevents showing errors when, for instance, useConfig is used, but user has not changed anything yet.
  • Considering above, 'always' value renamed to 'onAnyChange', keeping 'always' as valid value for backward compatibility.

v3.10.0

18 Apr 08:00
Compare
Choose a tag to compare

New Features

  • Added 'always' validate-on-change strategy. Setting onChangeStrategy validation config option to this value would result in inputs to be validated immediately on any change, potentially showing errors without waiting for user to finish their input. Use with caution.

v3.9.1

09 Apr 20:03
Compare
Choose a tag to compare

Patch Description

  • Updated React-related peerDependencies in package.json to cover latest (18.0.0) version of React without errors and warnings.

v3.9.0

09 Apr 19:51
Compare
Choose a tag to compare

New Features

  • Added asynchronous validation functionality requested by @Swensson in #1 . The details on new feature can be read in README. A quick example:
defValidation('checkEmail', (value, { message }) => {
  if (!value) return;

  return apiClient.checkEmail(value)
    .then((data) => {
      if (!data.isValid) {
        return Promise.reject(message || data.message || 'This email cannot be used');
      }
    })
});

function UserForm() {
  const { $ } = useForm({
    initial: { email: '', fullName: '' },
    validations: {
      rules: {
        email: 'presence',
        fullName: 'presence'
      },
      async: {
        email: 'checkEmail'
      }
    }
  });
}

v3.8.0

23 May 19:28
Compare
Choose a tag to compare

New Features

  • Added onSet property to FormProvider when used in controlled fashion. This allows to dispatch "set form attributes" action with additional options. The only supported option is validate, which allows to skip validation when setting new attributes, and form is in "validate on change" state.
const onSet = useCallback((setAttrs) => {
  setAttrs({ validate: false });
}, []);

return (
  <FormProvider
    attrs={attrs}
    onChange={onChange}
    onSet={onSet}
  >
    <OrderForm />
  </FormProvider>
);

v3.7.0

21 Feb 20:47
Compare
Choose a tag to compare

New Features

  • Added validation rule wildcard index captures for validating collection items. A rule named items.(index).value follows the same validation logic as items.*.value, but also will pass index property with corresponding collection index to validator function (see example bellow). This is especially handy when item's validation depends on other items in the collection.
  • Added pinned validation dependencies. A validation like items.*.max with dependency like items.*.min would mean that change of any "min" input in the collection will re-validate every "max" input. To avoid this behavior, dependency items.^.min may be used now. This means that if, for instance, items.1.min input changes, and the form is in "validate-on-change" state, only items.1.max input will be re-validated. For example:
function ItemsForm() {
  const { $ } = useForm({
    validations:  {
      'items.*.min': 'presence',
      'items.(index).max': {
        rules: [
          'presence',
          (value, { index, attrs }) => {
            if (value <= attrs.items[index].min) {
              return 'Has to be greater than "min";
            }
          }
        ],
        deps: ['items.^.min']
      }
    }
  });
  // ...
}

Other Improvements

  • helpers config option can now accept an array of helpers-generating functions for convenience.

Fixes

  • Fixed a bug when validation of items.1.something validation rule would also trigger items.*.something rule validation for items.1.something input, which causes a problem when collection items has none or one element, and thus validations of items.*.something rule should not reach non-existing input.

v3.6.2

11 Feb 01:17
Compare
Choose a tag to compare

Patch Description

  • Fixed "all-wildcard" paths validation that got broken after fixing "semi-wildcard paths validation" in v3.6.1.
  • Fixed expected behavior when an input matches multiple wildcard validation rules, like foos.1.bars.*.value and foos.*.bars.*.value: all of them will be called

v3.6.1

16 Nov 23:06
Compare
Choose a tag to compare

Patch Description

  • Fixed validation for "semi-wildcard" paths, i.e. ones that contain both explicit indices and wildcards, like 'items.2.nested.*.value'. The flaw in validator lookup only allowed to use only all-explicit or all-wildcard paths. Issue reported by @MrFreemind .

v3.6.0

01 Nov 22:33
Compare
Choose a tag to compare

New Features

  • Added validations.onChangeStrategy config option. This option allows to set on-change behavior of inputs. 'onAnyError' value, which is default one, will validate inputs only if there is any error present on the form. On other hand, 'onAfterValidate' value will validate inputs if validate form helper has been called. 'none' strategy allows to avoid on-change validation entirely. Note, however, that input error will still be cleared when it's changed.
  • Added 'validations.<input>.partialDeps config option for usePartial hook. It is similar to deps, but will prefix each value with partial's prefix to make things less verbose.

Improvements

  • Upon validation, form's errors object no longer contains falsy values (i.e. only keys with actual errors will be present). Improvement suggested by @MrFreemind

Fixes

  • When setting a collection of items via set helper, when each of this items is represented with it's own validations defined via dynamic config or usePartial hook, additional check has been added to avoid validation of about-to-go-away items. This validation exists on set phase because items with their validation setup will only go away after next render and all related effect. Issue reported by @MrFreemind

v3.5.2

24 Sep 20:37
Compare
Choose a tag to compare

Patch Description

  • Fixed inconsistency of usePartial's set helper method: it is now able to accept a function as parameter to have consistent behavior with top-level set function.