Socket
Book a DemoInstallSign in
Socket

@avinlab/form

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@avinlab/form

## Install

0.3.2
latest
npmnpm
Version published
Weekly downloads
7
600%
Maintainers
1
Weekly downloads
 
Created
Source

@avinlab/form

Install

npm install @avinlab/form

Usage

Create a form and manage its values:

import { createForm } from '@avinlab/form';

// Create a form with initial values
const initialValues = { name: 'John', age: 30 };
const form = createForm(initialValues);

// Subscribe to updates
const handleUpdateForm = (newValues, prevValues) => {
  console.log('Form updated', { newValues, prevValues });
};
form.onUpdate(handleUpdateForm);

const handleUpdateAgeField = (newValue, oldValue) => {
  console.log('Field "age" updated', { newValue, oldValue });
};
form.onUpdateField('age', handleUpdateAgeField);

// Set form values
form.setValue('name', 'Jane');
form.setValue('age', 31);

// Set all form values at once
form.setValues({ name: 'Jane', age: 31 });

// Unsubscribe from updates
form.offUpdate(handleUpdateForm);
form.offUpdateField('age', handleUpdateAgeField);

Form validation management:

import { createForm, createFormValidation } from '@avinlab/form';

const initialValues = { name: 'John', age: 30 };
const form = createForm(initialValues);

const formValidation = createFormValidation(form, (values) => {
  const errors = {};
  if (!values.name) {
    errors.name = 'Name is required';
  }
  if (values.age && values.age < 18) {
    errors.age = 'Must be at least 18';
  }
  return errors;
});

console.log(formValidation.errors); // Output: {}
console.log(formValidation.isValid); // Output: true

// Set an incorrect value for the age field
form.setValue('age', 15);

console.log(formValidation.errors); // Output: {age: 'Must be at least 18'}
console.log(formValidation.isValid); // Output: false

// Subscribe to validation changes
const handler = (errors) => {
  console.log(errors);
};
formValidation.onValidate(handler);

// Unsubscribe from validation updates
formValidation.offValidate(handler);

// Validation is triggered automatically when the form values change,
// but you can also initiate validation manually
formValidation.validate();

Keywords

form

FAQs

Package last updated on 05 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.