New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
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.2.0
npm
Version published
Weekly downloads
0
-100%
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);

// 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 09 Dec 2023

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