Releases: akuzko/re-use-form
Releases · akuzko/re-use-form
v3.10.1
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
New Features
- Added
'always'
validate-on-change strategy. SettingonChangeStrategy
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
v3.9.0
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
New Features
- Added
onSet
property toFormProvider
when used in controlled fashion. This allows to dispatch "set form attributes" action with additional options. The only supported option isvalidate
, 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
New Features
- Added validation rule wildcard index captures for validating collection items. A rule named
items.(index).value
follows the same validation logic asitems.*.value
, but also will passindex
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 likeitems.*.min
would mean that change of any "min" input in the collection will re-validate every "max" input. To avoid this behavior, dependencyitems.^.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, onlyitems.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 triggeritems.*.something
rule validation foritems.1.something
input, which causes a problem when collectionitems
has none or one element, and thus validations ofitems.*.something
rule should not reach non-existing input.
v3.6.2
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
andfoos.*.bars.*.value
: all of them will be called
v3.6.1
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
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 ifvalidate
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 forusePartial
hook. It is similar todeps
, 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 orusePartial
hook, additional check has been added to avoid validation of about-to-go-away items. This validation exists onset
phase because items with their validation setup will only go away after next render and all related effect. Issue reported by @MrFreemind