Search for a command to run...
Last updated June 9, 2026
Ctrovalidate provides methods to manage the lifecycle of form fields that are added or removed from the DOM after initial controller initialization.
addField(element: HTMLElement)Registers a new input for validation tracking.
Behavior:
data-ctrovalidate-rules and schema-based rules.realTime is enabled for the instance.const input = document.createElement('input');
input.name = 'new_field';
input.setAttribute('data-ctrovalidate-rules', 'required');
container.appendChild(input);
validator.addField(input);removeField(element: HTMLElement)Unregisters an input and cleans up all associated resources.
Behavior:
blur and input listeners.const field = document.querySelector('[name="obsolete_field"]');
validator.removeField(field);
field.remove();refresh()Re-scans the entire form container to synchronize the field list with the current DOM state.
Behavior:
data-ctrovalidate-rules attributes and schema-based fields.// Bulk DOM update
container.innerHTML = `<input name="f1" data-ctrovalidate-rules="required">`;
validator.refresh();When using reactive frameworks, triggers methods within component lifecycle hooks to maintain synchronization.
useEffect(() => {
// Sync on mount or dependency change
validator.refresh();
return () => validator.destroy();
}, [dependencies]);onMounted(() => {
const validator = new Ctrovalidate(formRef.value);
});
// Sync after DOM updates
watch(items, async () => {
await nextTick();
validator.refresh();
});addField or refresh.removeField before removing elements from the DOM to ensure event listeners are detached correctly.refresh() when multiple fields are modified simultaneously to avoid redundant discovery cycles.