
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
hook-form-validator
Advanced tools
This is the Validator hook to easily and fastest way to validate the fields in React forms
A fast and lightweight,free and open-source validation library for ReactJS that addresses three primary form creation pain points: State manipulation. Form Validation (error message) Form Submission.
This library consists wrapper React components over hook-form-validator library.
Show
npm i hook-form-validator
npm i deepmerge
import React, { Component } from 'react';
import * as Yup from "yup";
import useValidator from 'hook-form-validator';
export default function App() {
const onSubmit = () => {
// here you will get the validated data
console.log(values)
}
const {
values,
setValues,
touched,
errors,
handleSubmit,
getFieldProps,
} = useValidator({
initialValues: {
username: "",
email: "",
},
validationSchema: Yup.object({
username: Yup.string().required("User Name is Required."),
email: Yup.string().email().required("Email is Required."),
}),
onSubmit,
});
return (
<>
<form onSubmit={handleSubmit}>
<div>
<input type="text" name="username" {...getFieldProps("username")} />
{(touched?.username && errors?.username) && <div>{errors?.username}</div>}
</div>
<div>
<input type="text" name="email" {...getFieldProps("email")} />
{(touched?.email && errors?.email) && <div>{errors?.email}</div>}
</div>
<button type="submit">
Save
</button>
</form>
</>
);
}
Component that contains the draggable elements or components. Each of its children should be wrapped by Draggable component
| Property | Type | Default | Description |
|---|---|---|---|
| values | object | {} | Get updated values |
| errors | object | {} | Get all the errors |
| touched | object | {} | It helps to display error based on blur event |
| setValues | function | undefined | Update the values |
| getFieldProps | function | undefined | You can directly pass this function for handle the value |
| setErrors | function | undefined | It helps to update errors manually. |
| handleChange | function | undefined | It trigger the manually onChange function with help of createFakeEvent |
| handleBlur | function | undefined | It trigger the manually onBlur function with help of createFakeEvent |
| handleSubmit | function | undefined | You have pass this function in form tag |
| createFakeEvent | function | undefined | It will helps to trigger fake event |
You have pass this function in form tag with the help of this function it trigger when submit button is clicked & check for errors & trigger the onSubmit function which is provided by our hook
You can see the example to use handleSubmit
handleSubmitThis function use to update the initialValues
setValue({...values,[key]:[value]})
const App = () => {
const {
setValues,
handleSubmit,
getFieldProps,
} = useValidator({
initialValues: {
firstName: "",
},
validationSchema: Yup.object({
firstName: Yup.string().required("User Name is Required."),
}),
onSubmit,
});
return (
<form>
<input {...getFieldProps("firstName")} />
<button type="button" onClick={() => setValue("firstName", "Bill")}>
setValue
</button>
</form>
);
};
objectYou can directly pass this function for handle the value
<input {...getFieldProps('firstName')} />
ChangeHandler onChange prop to subscribe the input change event.ChangeHandler onBlur prop to subscribe the input blur event.React.Ref<any> Input reference for hook form to register.string Input's name being registered.The function allows you to manually set one or more errors with your custom message.
setErrors({...errors,[key]:[message]})
errors Set an error with its type and message.It trigger the manually onChange function with help of createFakeEvent.
<input
onChange={(e) => {
handleChange("firstName")(createFakeEvent(e));
}}
/>
handleChangeIt trigger the manually onBlur function with help of createFakeEvent.
<input
onChange={(e) => {
handleBlur("firstName")(createFakeEvent(e));
}}
/>
handleBlurFAQs
This is the Validator hook to easily and fastest way to validate the fields in React forms
We found that hook-form-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.