/
githubmirror
/
react-hook-form
Обзор
Документация
Войти
/
githubmirror
/
react-hook-form
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/V6/validationSchema.tsx
56 строк
1 KB
Bill
🌐 prettier the entire code base (#5118)
10 май 2021, 10:18
Не верифицирован
10 май 2021, 10:18
10fe39d
Код
Авторство
О чём код?
import React from 'react'; import ReactDOM from 'react-dom'; import { useForm } from 'react-hook-form'; import * as yup from 'yup'; // you will have to install yup const SignupSchema = yup.object().shape({ firstName: yup.string().required(), age: yup.number().required().positive().integer(), website: yup.string().url(), }); export default function App() { const { register, handleSubmit, errors } = useForm({ validationSchema: SignupSchema, }); const onSubmit = (data) => { alert(JSON.stringify(data)); }; return ( <form onSubmit={handleSubmit(onSubmit)}> <div> <label>Develop?</label> Yes <input type="radio" name="test" value="yes" ref={register} /> No <input type="radio" name="test" value="no" ref={register} /> </div> <div> <label>First Name</label> <input type="text" name="firstName" ref={register} /> </div> <div> <label>Last Name</label> <input type="text" name="lastName" ref={register} /> </div> <div> <label>Age</label> <input type="text" name="age" ref={register} /> </div> <div> <label>Website</label> <input type="text" name="website" ref={register} /> </div> <div style={{ color: 'red' }}> <pre> {Object.keys(errors).length > 0 && ( <label>Errors: {JSON.stringify(errors, null, 2)}</label> )} </pre> </div> <input type="submit" /> </form> ); }