Skip to main content

Behaviors Overview

Behaviors add reactive logic to forms: computed fields, conditional visibility, field synchronization.

What Are Behaviors?​

Behaviors automatically react to form changes:

import { GroupNode } from '@reformer/core';
import { computeFrom, enableWhen } from '@reformer/core/behaviors';

const form = new GroupNode({
form: {
price: { value: 100 },
quantity: { value: 1 },
total: { value: 0 },
discount: { value: 0 },
showDiscount: { value: false },
},
behavior: (path) => {
// Auto-compute total
computeFrom([path.price, path.quantity], path.total, ({ price, quantity }) => price * quantity);

// Enable discount field conditionally
enableWhen(path.discount, (form) => form.total > 500);
},
});

Available Behaviors​

BehaviorDescription
computeFromCalculate field from other fields
transformValueTransform value on change
enableWhenConditional enable/disable
resetWhenReset field on condition
copyFromCopy value from another field
syncFieldsTwo-way synchronization
watchFieldReact to field changes
revalidateWhenTrigger revalidation

How Behaviors Work​

  1. Define in behavior
  2. ReFormer sets up reactive subscriptions
  3. When source fields change, behavior runs automatically
// When price or quantity changes β†’ total updates
computeFrom(
[path.price, path.quantity], // Watch these
path.total, // Update this
({ price, quantity }) => price * quantity // With this function
);

Behavior vs Validation​

AspectValidationBehavior
PurposeCheck correctnessReact to changes
OutputErrorsSide effects
ExamplesRequired, email formatComputed total, show/hide

Next Steps​