reack hook form validate 2 form code example
Example 1: react-hook-form material-ui
import React from "react";
import { useForm, Controller } from "react-hook-form";
import Select from "react-select";
import Input from "@material-ui/core/Input";
import { Input as InputField } from "antd";
export default function App() {
const { control, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Controller as={Input} name="HelloWorld" control={control} defaultValue="" />
<Controller as={InputField} name="AntdInput" control={control} defaultValue="" />
<Controller
as={Select}
name="reactSelect"
control={control}
onChange={([selected]) => {
return { value: selected };
}}
defaultValue={{}}
/>
<input type="submit" />
</form>
);
}
Example 2: react hooks form
import React from 'react';
import { useForm } from 'react-hook-form';
function App() {
const { register, handleSubmit, errors } = useForm();
const onSubmit = (data) => {
console.log(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<input name="firstname" ref={register} /> {}
<input name="lastname" ref={register({ required: true })} />
{errors.lastname && 'Last name is required.'}
<input name="age" ref={register({ pattern: /\d+/ })} />
{errors.age && 'Please enter number for age.'}
<input type="submit" />
</form>
);
}