Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@uplink-protocol/form-controller

jmkcoder666MIT0.2.6TypeScript support: included

Reactive multi-step form controller with dynamic validation and state management

form, controller, validation, multi-step, wizard, react, forms, uplink-protocol

readme

@uplink-protocol/form-controller

A lightweight yet powerful form management system for building dynamic, multi-step forms with advanced validation capabilities. This module is part of the Odyssey Uplink Protocol.

License: MIT

Features

  • Flexible Form Structure - Support for both single-step and multi-step forms
  • Built-in Validation - Comprehensive validation rules out of the box
  • Dynamic Validation - Context-aware validators that can react to other field values
  • Enhanced Validation - Support for multiple validation errors and per-validation error messages
  • Reactive State Management - Subscribe to state changes for reactive UI updates
  • Progressive Form Building - Add or remove steps dynamically
  • Framework Agnostic - Works with any UI library or vanilla JavaScript

Installation

npm install @uplink-protocol/form-controller

Basic Usage

// Import the form controller
const { FormController } = require('@uplink-protocol/form-controller');

// Define your form configuration
const formConfig = {
  steps: [
    {
      id: 'contact',
      title: 'Contact Information',
      fields: {
        name: {
          id: 'name',
          type: 'text',
          label: 'Full Name',
          required: true,
          value: ''
        },
        email: {
          id: 'email',
          type: 'email',
          label: 'Email Address',
          required: true,
          value: '',
          validation: {
            pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
          }
        }
      }
    }
  ]
};

// Initialize the form controller
const form = FormController(formConfig);

// Update field values
form.methods.updateField('contact', 'name', 'John Doe');

// Validate the form
if (form.methods.validateForm(true)) {
  // Form is valid, get the data
  const formData = form.methods.getFlatData();
  console.log('Form data:', formData);
}

Multi-step Form Example

const wizardForm = FormController({
  steps: [
    {
      id: 'personal',
      title: 'Personal Details',
      fields: {
        // Personal info fields
      }
    },
    {
      id: 'address',
      title: 'Address Information',
      fields: {
        // Address fields
      }
    },
    {
      id: 'preferences',
      title: 'User Preferences',
      fields: {
        // Preferences fields
      }
    }
  ]
});

// Navigate between steps
wizardForm.methods.nextStep();  // Advance to next step if current is valid
wizardForm.methods.prevStep();  // Go back to previous step
wizardForm.methods.goToStep(1); // Jump to specific step

// Check current status
const isLastStep = wizardForm.bindings.isLastStep.current;
const isFormValid = wizardForm.bindings.isFormValid.current;

Dynamic Validation

// Register a custom validator
form.methods.registerValidator('matchesPassword', (value, context) => {
  if (value !== context.formData.password) {
    return 'Passwords do not match';
  }
  return true;
});

// Use in a field definition
const confirmPasswordField = {
  id: 'confirmPassword',
  type: 'password',
  label: 'Confirm Password',
  validation: {
    dynamicValidator: 'matchesPassword'
  }
};

Enhanced Validation

// Field with per-validation error messages
const passwordField = {
  id: 'password',
  type: 'password',
  label: 'Password',
  validation: {
    required: true,
    minLength: 8,
    pattern: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/,
    // Specific error messages for each validation rule
    errorMessages: {
      required: 'Password is required',
      minLength: 'Password must be at least 8 characters long',
      pattern: 'Password must include uppercase, lowercase, and numbers'
    },
    // Enable multiple error collection
    collectAllErrors: true
  }
};

// Field with multiple dynamic validators
const usernameField = {
  id: 'username',
  type: 'text',
  label: 'Username',
  validation: {
    required: true,
    // Multiple dynamic validators with custom parameters
    dynamicValidators: [
      {
        name: 'uniqueUsername', 
        params: { checkDatabase: true },
        errorMessage: 'This username is already taken'
      },
      {
        name: 'allowedCharacters',
        errorMessage: 'Username can only contain letters, numbers, and underscores'
      }
    ],
    collectAllErrors: true
  }
};

Reactive UI Updates

// Subscribe to form data changes
form.bindings.formData.subscribe(data => {
  console.log('Form data updated:', data);
  updateUI(data);
});

// Subscribe to validation errors
form.bindings.fieldErrors.subscribe(errors => {
  displayErrors(errors);
});

Documentation

For detailed documentation and examples, see:

Use Cases

  • Multi-step Wizards - Build complex, multi-step registration or checkout flows
  • Dynamic Forms - Create forms that adapt based on user input
  • Complex Validation - Implement interdependent field validation rules
  • Form State Management - Maintain form state separate from UI components

License

MIT © Odyssey Uplink Protocol Team

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[0.2.6] - 2025-05-19

Added

  • ConfigManagerService with comprehensive configuration management capabilities
  • Dynamic step management with addStepWithFormData and removeStepWithCleanup methods
  • Full form reset functionality with updateConfigWithFullReset method

Changed

  • Improved internal state management when dynamically modifying form structure
  • Enhanced service coordination for configuration updates

[0.2.5] - 2025-05-19

Fixed

  • Additional fixes for module resolution in browser environments
  • Enhanced webpack configuration for better dependency bundling

[0.2.4] - 2025-05-19

Fixed

  • Fixed module resolution for @uplink-protocol/core in browser environments
  • Updated webpack configuration to properly bundle dependencies
  • Resolved "Failed to resolve module specifier" error

[0.2.3] - 2025-05-19

Changed

  • Updated to @uplink-protocol/core v0.0.9

[0.2.2] - 2025-05-19

Fixed

  • Various bug fixes and performance improvements

[0.2.1] - 2025-05-19

Changed

  • Migrated to @uplink-protocol/core for reactive bindings system
  • Improved test suite organization

Fixed

  • Jest configuration for external dependencies
  • Test compatibility with new binding system

[0.2.0] - 2025-05-18

Added

  • Enhanced validation capabilities with multiple error collection
  • Improved dynamic form modification with better step management
  • Additional bindings for reactive form state tracking

Changed

  • Optimized form state updates for better performance
  • Improved error message handling and customization
  • Enhanced service architecture for better maintainability

Fixed

  • Various edge cases in multi-step form validation
  • Issues with dynamic validator registration

[0.1.1] - Initial Release

Added

  • Initial implementation of form controller
  • Basic multi-step form support
  • Field-level and form-level validation
  • Reactive bindings system