Skip to main content

revalidateWhen()

function revalidateWhen<TForm>(
target,
triggers,
options?): void;

Defined in: core/behavior/behaviors/revalidate-when.ts:65

Перевалидирует поле при изменении других полей

Type Parameters

TForm

TForm

Parameters

target

FieldPathNode<TForm, FormValue>

Поле для перевалидации

triggers

FieldPathNode<TForm, FormValue, unknown>[]

Поля-триггеры (НЕ должно содержать target)

options?

RevalidateWhenOptions

Опции (debounce)

Returns

void

Examples

import { revalidateWhen, type BehaviorSchemaFn } from '@reformer/core/behaviors';
import { equalTo } from '@reformer/core/validators';
import type { FieldPath } from '@reformer/core';

interface RegistrationForm { password: string; confirmPassword: string }

export const validation = (path: FieldPath<RegistrationForm>) => {
equalTo(path.confirmPassword, path.password, { message: 'Пароли не совпадают' });
};

export const behavior: BehaviorSchemaFn<RegistrationForm> = (path) => {
// Если пользователь сначала ввёл confirm, потом меняет password —
// без revalidateWhen ошибка confirmPassword останется устаревшей.
revalidateWhen(path.confirmPassword, [path.password]);
};
import { revalidateWhen, type BehaviorSchemaFn } from '@reformer/core/behaviors';

interface MortgageForm {
propertyValue: number;
loanAmount: number;
initialPayment: number; // правило: initialPayment >= propertyValue * 0.2 - loanAmount
}

export const mortgageBehavior: BehaviorSchemaFn<MortgageForm> = (path) => {
revalidateWhen(
path.initialPayment,
[path.propertyValue, path.loanAmount],
{ debounce: 300 }, // не дёргаем сервер на каждый keystroke
);
};

See

docs/llms/27-revalidate-when.md