Powerful, lightweight, and declarative form control library.
FormFx allows you to manage dynamic form behaviors (show/hide, enable/disable, validation) using simple data attributes or JSON rules, without writing complex JavaScript or relying on heavy frameworks.
- Better Maintenance: Avoid jQuery/Vanilla JS spaghetti code.
- Lightweight: Minimal overhead compared to full-blown form engines.
- Declarative: Logic is co-located with HTML using
data-fx-*attributes. - Safe: No
eval()ornew Function(). Expressions are evaluated safely. - Framework Agnostic: Works everywhere.
npm install formfxOr via CDN:
<script src="https://unpkg.com/formfx/dist/index.js"></script>Just define rules in your HTML using data attributes.
<form id="my-form">
<input type="checkbox" id="toggle" name="toggle">
<div data-fx-show="toggle == true">
<p>This is only visible when the checkbox is checked.</p>
<input type="text" name="optional_field" data-fx-required="toggle == true">
</div>
</form>
<script type="module">
import { FormFx } from 'formfx';
const fx = new FormFx(document.getElementById('my-form'));
fx.mount();
</script>For more complex logic or when you want to keep HTML clean. JSON rules take precedence over attributes.
const fx = new FormFx(form, {
rules: [
{
if: "total > 1000",
then: [
{ show: "#discount-section" },
{ required: "#coupon-code" }
]
}
]
});Handle dynamic rows with ease.
<div data-fx-repeater="items" data-fx-min="1" data-fx-max="5">
<div data-fx-list>
<template>
<div data-fx-item>
<input type="text" data-fx-field="name">
<button type="button" data-fx-remove-btn>Remove</button>
</div>
</template>
</div>
<button type="button" data-fx-add-btn>Add Item</button>
</div>Refer to fields within the same repeater row.
<input type="number" data-fx-field="price">
<div data-fx-show="@row.price > 100">Expensive item!</div>Automatically save and restore form rules state.
const fx = new FormFx(form, {
persist: {
key: 'my-form-settings',
storage: 'localStorage'
}
});A visual editor for your form rules.
import { FormFx } from 'formfx';
// Import editor separately to keep main bundle small
import { RuleEditor } from 'formfx/editor';
import 'formfx/editor.css';
const fx = new FormFx(form);
fx.mount();
const editor = new RuleEditor(fx, {
mount: document.getElementById('editor-container'),
mode: 'json'
});
editor.mount();Visual debug information for development.
const fx = new FormFx(form, { debug: true });disableOnHide: Boolean (defaulttrue)clearOnHide: Boolean (defaultfalse)rules:JSONRule[]persist:PersistOptionsdebug: Boolean
mount(): Initialize listeners and observers.destroy(): Cleanup everything (removes listeners and observers).pause()/resume(): Pause or resume rule evaluation.reEvaluate(): Manually trigger evaluation.exportRules(): Get current JSON rules.importRules(rules): Load new JSON rules.enableRule(ruleId)/disableRule(ruleId): Control JSON rules by ID.
- Modern browsers (Chrome, Firefox, Safari, Edge)
- No
eval()used. Safe for strict CSP environments.
- Custom tokenizer and parser ensure expressions are only allowed to perform specific safe operations.
- No access to global objects like
windowordocumentfrom within expressions.
- v1.1: Async function support in expressions.
- v2.0: Plugin system for custom effects.
This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
By contributing to this repository, you agree to the terms of our Contributor License Agreement (CLA), which includes the waiver of moral rights and allows for future re-licensing or commercial use by the project owner.